使用 Entity Framework 4.1 Code First Fluent API 配置具有基于其他两个实体的复合键的实体

发布于 2024-11-27 21:04:41 字数 1411 浏览 1 评论 0原文

我刚刚开始掌握实体框架,并且在将简单实体映射到各自的表方面没有任何问题,但现在我遇到了一个更高级的场景,这让我感到困惑。

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2024-12-04 21:04:41

试试这个:

modelBuilder.Entity<EventTypePreference>()
            .HasKey(p => new { p.EventTypeId, p.GroupId });

modelBuilder.Entity<EventTypePreference>()
            .HasRequired(p => p.EventType)
            .WithMany()
            .HasForeignKey(p => p.EventTypeId);

modelBuilder.Entity<EventTypePreference>()
            .HasRequired(p => p.Group)
            .WithMany(g => g.Preferences)
            .HasForeignKey(p => p.GroupId);

Try this:

modelBuilder.Entity<EventTypePreference>()
            .HasKey(p => new { p.EventTypeId, p.GroupId });

modelBuilder.Entity<EventTypePreference>()
            .HasRequired(p => p.EventType)
            .WithMany()
            .HasForeignKey(p => p.EventTypeId);

modelBuilder.Entity<EventTypePreference>()
            .HasRequired(p => p.Group)
            .WithMany(g => g.Preferences)
            .HasForeignKey(p => p.GroupId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文