使用 Entity Framework 4.1 Code First Fluent API 配置具有基于其他两个实体的复合键的实体
我刚刚开始掌握实体框架,并且在将简单实体映射到各自的表方面没有任何问题,但现在我遇到了一个更高级的场景,这让我感到困惑。
我有以下 POCO
public class Group
{
public int GroupId {get; set;}
public string GroupName {get; set;}
public virtual ICollection<EventTypePreference> Preferences {get; set;}
}
public class EventType
{
public int EventTypeId {get; set;}
public string EventTypeName {get; set;}
public string EventColor {get; set;}
}
public class EventTypePreference
{
public int GroupId {get; set;}
public int EventTypeId {get; set;}
public virtual Group Group {get; set;}
public virtual EventType EventType {get; set;}
public int EventLength {get; set;}
}
在我的模型中,一个组将有许多 EventTypePreferences(每个 EventType 一个首选项记录)。 EventTypePreferences 是数据库中的一个表,其匹配列与其对应的 POCO 相同。此外,在数据库中,EventTypePreference 表使用基于 GroupId 和 EventTypeId 的复合主键。
假设 Group 表具有以下行(GroupId、GroupName)...
1, Human Resources
2, Purchasing
3, Information Services
并且 EventType 表具有以下行(EventTypeId、EventTypeName、EventColor)...
1, Training Event, Blue
2, Sales Seminar, Red
3, Office Party, Yellow
EventTypePreferences 的值为(EventTypeId、GroupId、EventLength)。我
1, 1, 60
1, 2, 45
1, 3, 60
2, 1, 120
.........
的问题是我正在努力弄清楚如何流畅地配置这些关系,特别是对于我的 EventTypePreference 实体。我看到它是因为该实体中有许多组和事件类型。有人可以帮我解释一下吗?
I've just started to get a grasp of Entity Framework and have had no problems with the mapping of simple entities to their individual tables, but now I've run across a more advance scenario that has me stumped.
I have the following POCOs
public class Group
{
public int GroupId {get; set;}
public string GroupName {get; set;}
public virtual ICollection<EventTypePreference> Preferences {get; set;}
}
public class EventType
{
public int EventTypeId {get; set;}
public string EventTypeName {get; set;}
public string EventColor {get; set;}
}
public class EventTypePreference
{
public int GroupId {get; set;}
public int EventTypeId {get; set;}
public virtual Group Group {get; set;}
public virtual EventType EventType {get; set;}
public int EventLength {get; set;}
}
In my model a Group will have many EventTypePreferences (one preference record for each EventType). EventTypePreferences is a table in the database with the same matching columns as it's corresponding POCO. Also, in the database the EventTypePreference table uses a composite primary key that is based on the GroupId and the EventTypeId.
So say the Group table has the following rows (GroupId, GroupName)...
1, Human Resources
2, Purchasing
3, Information Services
And the EventType table has the following rows (EventTypeId, EventTypeName, EventColor)...
1, Training Event, Blue
2, Sales Seminar, Red
3, Office Party, Yellow
The EventTypePreferences would have values as (EventTypeId, GroupId, EventLength)...
1, 1, 60
1, 2, 45
1, 3, 60
2, 1, 120
.........
My problem is that I am struggling to figure out how to fluently configure these relationships particularly for my EventTypePreference entity. I'm seeing it as there are many Groups and EventTypes in this entity. Can someone help shed some light to this for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: