entity framework code first 设置外键的主从关系问题

在使用Entity Framework Code First时,设置外键以建立主从关系的关键在于源表中声明`ICollection`,这表明另一表对此表的引用。而在被引用的表中,通过声明`virtual`类型的表实体实现延迟加载,以减少资源消耗。
摘要由CSDN通过智能技术生成
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值