Why does EF5 create foreign keys with _Id, but recommend naming convention Id without the underscore?

前端 未结 1 385
清歌不尽
清歌不尽 2020-12-20 04:57

I have columns in my database that EF put in for me, that are classname followed by _Id. However much of the documentation recommends that the naming convention for foreign

相关标签:
1条回答
  • 2020-12-20 05:34

    Every entity that holds a reference to another entity should have a property named ForeignEntityId (note the lower case d in Id).

    If you set up your entities following the exact pattern of this example, all your columns should be named according to convention:

    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public virtual ICollection<Order> Orders { get; set; }
    }
    
    public class Order
    {
        public int Id { get; set; }
        public int CustomerId { get; set; } // this property is important
        public string ProductName { get; set; }
        public decimal Price { get; set; }
    
        public virtual Customer Customer { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题