为了账号安全,请及时绑定邮箱和手机立即绑定

EF code first 不能自动生成外键实体

EF code first 不能自动生成外键实体

弑天下 2018-12-07 01:41:32
public class Product : EntityBase<int>, IAggregateRoot, IProductAttribute { public string Name { get; set; } public decimal Price { get; set; } public int SalesAmount { get; set; } public string Introduction { get; set; } public Brand Brand { get; set; } public Category Category { get; set; } protected override void Validate() { throw new NotImplementedException(); } public override string ToString() { return this.Name; } } 查询生成Product实体时 属性Category是为null的,在数据库中Category和Product是一对多关系,请问怎么样才能自动生成Category。 Ps:数据库是我自己手动创建的,然后用code first匹配的  
查看完整描述

2 回答

?
森林海

TA贡献2011条经验 获得超2个赞

public virtual Category Category{set;get;}
public virtual int? CategoryId{set;get;}
查看完整回答
反对 回复 2019-01-21
?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

查询时需要.Include(p=>p.Category)

查看完整回答
反对 回复 2019-01-21
  • 2 回答
  • 0 关注
  • 464 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信