public class Persons
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PersonId { get; set; }
[MaxLength(20)]
public string Name { get; set; }
[MaxLength(11)]
public string Phone { get; set; }
[MaxLength(20)]
public string Wechat { get; set; }
public bool RecvMsg { get; set; }
public bool RecvWx { get; set; }
// 外键(出)
public ICollection<Buildings> Buildings { get; set; }
}
public class Buildings
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int BuildingId { get; set; }
public int ParentId { get; set; }
[Required]
public string Caption { get; set; }
public int DeviceId { get; set; }
[ForeignKey("Persons")]
public int PersonId { get; set; }
[DecimalPrecision(5, 2)]
public decimal MinTem { get; set; }
[DecimalPrecision(5, 2)]
public decima
entity framework code first 设置外键的主从关系问题
最新推荐文章于 2022-03-14 10:56:49 发布
在使用Entity Framework Code First时,设置外键以建立主从关系的关键在于源表中声明`ICollection`,这表明另一表对此表的引用。而在被引用的表中,通过声明`virtual`类型的表实体实现延迟加载,以减少资源消耗。
摘要由CSDN通过智能技术生成