ASP MVC 4首先映射一对一关系代码

时间:2013-04-28 18:14:31

标签: asp.net-mvc-4 ef-code-first

我是ASP MVC的新手,正在开发一个具有复杂相关数据模型的项目。因此,在处理关系时,我在网上看了一下,并在asps.net的博客上得到了以下示例:

namespace CodeFirst.Associations.OneToOneFK
{
    public class User
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }        
        public int BillingAddressId { get; set; }
        public int DeliveryAddressId { get; set; }       

        public Address BillingAddress { get; set; }
        public Address DeliveryAddress { get; set; }
    }

    public class Address
    {
        public int AddressId { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }   
    }
}

所以我怀疑我们是否真的需要int BillingAddressID和Address BillingAddress? 另外,如果我们不使用AddressID,我们如何将地址与用户关联。

感谢您的帮助。 :)

1 个答案:

答案 0 :(得分:2)

int BillingAddressID被称为外键属性。

BillingAddress称为导航属性(在本例中为引用导航属性)。

定义关系不需要外键属性,但它们确实简化了某些编码模式。一般建议是使用导航属性和外键属性。

有关为何引入FK关联的详细信息,请参阅here

相关问题