实体框架CTP4和组合键 [英] Entity Framework CTP4 and composite keys

查看:219
本文介绍了实体框架CTP4和组合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用的EntityFramework CTP4玩,并决定将它应用到我目前的项目之一。该应用程序使用SQLServer数据库,并有一个表,一个复合键。再说了,表myEntity所拥有密钥1和密钥2既是外键(单独),并作为一个复合主键。

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.

我从派生的配置类EntityConfiguration:

I made a configuration class derived from EntityConfiguration :

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

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

Then in my DataContext (derived from DbContext) :

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

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



现在的问题是,当我查询MyEntities以其所有记录:

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

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



我得到一个非常怪异的结果组成的第一条记录重复18次,然后第二个重复18次(备案,我的表有36条记录)。

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 :)

推荐答案

我没有测试它我当前的数据库和放大器; CTP4但在CTP3你创建一个这样的组合键:

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 });

这篇关于实体框架CTP4和组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆