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

    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匹配的
 
弑天下
浏览 465回答 2
2回答

森林海

public virtual Category Category{set;get;} public virtual int? CategoryId{set;get;}

慕桂英3389331

查询时需要.Include(p=>p.Category)
打开App,查看更多内容
随时随地看视频慕课网APP