实体框架 CTP4 和组合键

发布于 2024-09-11 03:16:12 字数 930 浏览 9 评论 0原文

我正在使用 EntityFramework CTP4,并决定将其应用到我当前的项目之一。该应用程序使用 SQLServer 数据库,并且有一个带有组合键的表。假设表“MyEntity”将“Key1”和“Key2”既作为外键(单独)又作为复合主键。

我创建了一个从 EntityConfiguration 派生的配置类:

class MyEntityConfiguration : EntityConfiguration<MyEntity>
{
    public MyEntityConfiguration()
    {
        HasKey(m => m.Key1);
        HasKey(m => m.Key2);
    }
}

然后在我的 DataContext(从 DbContext 派生)中:

    public DbSet<MyEntity> MyEntities { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new MyEntityConfiguration());
    }

问题是,当我查询“MyEntities”的所有记录时:

var entities = from e in MyModel.Instance.MyEntities
               select e;

我得到一个非常奇怪的结果,其中第一个记录重复了 18 次,然后第二个重复 18 次(郑重声明,我的表有 36 条记录)。

我怀疑问题出在复合键上,因为没有其他实体显示此问题。

任何帮助将不胜感激,谢谢:)

I was playing with EntityFramework CTP4 and decided to apply it to one of my current projects. The application uses a SQLServer database and there is one table with a composite key. Say, table "MyEntity" has "Key1" and "Key2" as both foreign keys (individually) and as a composite primary key.

I made a configuration class derived from EntityConfiguration :

class MyEntityConfiguration : EntityConfiguration<MyEntity>
{
    public MyEntityConfiguration()
    {
        HasKey(m => m.Key1);
        HasKey(m => m.Key2);
    }
}

Then in my DataContext (derived from DbContext) :

    public DbSet<MyEntity> MyEntities { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new MyEntityConfiguration());
    }

The problem is that when I query "MyEntities" for all its records :

var entities = from e in MyModel.Instance.MyEntities
               select e;

I get a really weird result consisting of the first record repeated 18 times, then the second repeated 18 times (for the record, my table has 36 records).

I suspect the problem is with the composite key as no other entity is showing this problem.

Any help would be appreciated, thanks :)

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

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

发布评论

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

评论(1

兰花执着 2024-09-18 03:16:12

我还没有用我当前的数据库进行测试CTP4 但在 CTP3 中您将创建一个如下所示的复合密钥:

HasKey(m => new { m.Key1, m.Key2 });

I haven't tested it with my current database & CTP4 but in CTP3 you'd create a composite key like this:

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