开发者

Relationship Using Code first with Existing database

开发者 https://www.devze.com 2023-04-09 01:59 出处:网络
When defining a relationship between two types is it important to include a navigation property on both types, such as in the following example:

When defining a relationship between two types is it important to include a navigation property on both types, such as in the following example:

    public class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public Category Category { get; set; }
    }

    public class Category
    {
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public ICollection<Product> Products { get; set; }
    }

Can I do without inclu开发者_JAVA百科ding the navigation property in Category ?


If you just want it infered by code first convention then yes you need both on either side. I'd also make the collection "virtual" to support lazy loading.

You can set it up using the fluent configuration when the model is built. It would be something like this

modelBuilder.Entity<Product>()
    .HasMany(x => x.Category) 
0

精彩评论

暂无评论...
验证码 换一张
取 消