使用EntityFramework时出现关联问题

发布于 2024-10-30 17:27:23 字数 287 浏览 0 评论 0原文

我目前正在测试 EntityFramework 4.1。

我的问题是,我有一个团队类和一个游戏类。

class Team {
    string Name { get; set; }
}

class Game {
     Team HomeTeam { get; set; }
     Team AwayTeam { get; set; }
}

仍然没有找到绘制此图的方法。我已经成功地使用 NHibernate 做到了这一点,但使用 EF 却没有成功。有什么指点吗?

I'm currently testing EntityFramework 4.1.

My problem is, I have a Team class and a Game class.

class Team {
    string Name { get; set; }
}

class Game {
     Team HomeTeam { get; set; }
     Team AwayTeam { get; set; }
}

Still haven't found a way to map this. I've managed to do so using NHibernate but no luck with EF. Any pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

爱给你人给你 2024-11-06 17:27:23

这对我来说是清晰的映射:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Team>().HasKey(t => t.Name);

        modelBuilder.Entity<Game>().HasKey(
                        g => new { g.AwayTeamName, g.HomeTeamName });

        modelBuilder.Entity<Game>()
                        .HasRequired(g => g.HomeTeam)
                        .WithMany()
                        .HasForeignKey(g => g.HomeTeamName)
                        .WillCascadeOnDelete(true);

        modelBuilder.Entity<Game>()
                        .HasRequired(g => g.AwayTeam)
                        .WithMany()
                        .HasForeignKey(g => g.AwayTeamName)
                        .WillCascadeOnDelete(false);

        base.OnModelCreating(modelBuilder);
    }

请注意,您不能同时级联 PK/FK 关系。如果您成功使用 NHib,您就会知道级联两者会导致循环。


编辑:我想我应该包括我的 POCO:

public class Team
{
    public string Name { get; set; }
}

public class Game
{
    internal string HomeTeamName { get; set; }
    public Team HomeTeam { get; set; }

    internal string AwayTeamName { get; set; }
    public Team AwayTeam { get; set; }
}

请注意,您的 POCO 上必须有某种密钥,因此需要 HomeTeamName 和 AwayTeamName。如果您想对使用代码隐藏它们,您可以将它们保留在内部。

This is maps cleanly for me:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Team>().HasKey(t => t.Name);

        modelBuilder.Entity<Game>().HasKey(
                        g => new { g.AwayTeamName, g.HomeTeamName });

        modelBuilder.Entity<Game>()
                        .HasRequired(g => g.HomeTeam)
                        .WithMany()
                        .HasForeignKey(g => g.HomeTeamName)
                        .WillCascadeOnDelete(true);

        modelBuilder.Entity<Game>()
                        .HasRequired(g => g.AwayTeam)
                        .WithMany()
                        .HasForeignKey(g => g.AwayTeamName)
                        .WillCascadeOnDelete(false);

        base.OnModelCreating(modelBuilder);
    }

Note that you cannot cascade both PK/FK relationships. If you were successful with NHib, you will know that cascading both would cause cycles.


EDIT: I guess I should include my POCOs:

public class Team
{
    public string Name { get; set; }
}

public class Game
{
    internal string HomeTeamName { get; set; }
    public Team HomeTeam { get; set; }

    internal string AwayTeamName { get; set; }
    public Team AwayTeam { get; set; }
}

Note that you MUST have some sort of key on your POCOs, hence the need for the HomeTeamName and AwayTeamName. You can keep them internal if you want to hide them from the consuming code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文