如何在 ASP.NET MVC 中映射多个一对多关系?
我有几个域模型 - Address
、Customer
、Employee
、StoreLocation
。 Address
与 Customer
和 Employee
具有多对一关系,与 StoreLocation 具有一对一关系。
public class Address
{
public int Id;
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Address> Addresses { get; set; }
}
public class StoreLocation
{
public int Id { get; set; }
public string ShortCode { get; set; }
public string Description { get; set; }
public Address Address { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Dob { get; set; }
public IList<Address> Addresses { get; set; }
}
如何映射这种关系?我正在使用 ASP.NET MVC 3.0 和 Entity Framework 4.1。
I have few Domain Models - Address
, Customer
, Employee
, StoreLocation
. Address
has many to one relationship with Customer
and Employee
and one to one relationship with StoreLocation.
public class Address
{
public int Id;
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Address> Addresses { get; set; }
}
public class StoreLocation
{
public int Id { get; set; }
public string ShortCode { get; set; }
public string Description { get; set; }
public Address Address { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Dob { get; set; }
public IList<Address> Addresses { get; set; }
}
How to Map this relationship?. I am using ASP.NET MVC 3.0 and Entity Framework 4.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用代码优先(我认为您想要这个,否则,您必须编辑您的 Q),第一种方法是下面解释的方法:
Entities:
DbContext继承类:
上下文初始化程序:
这将创建以下数据库模式:
如果您对任何部分有任何疑问或需要澄清,请告诉我。
If you are using code-first (I think you want this, else, you have to edit your Q), the first way is the way explained below:
Entities:
DbContext inherited class:
Context Initializer:
That will create the db-schema below:
Let me know if you have any questions or need clarifications on any part.