实体框架 CTP4 和组合键
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有用我当前的数据库进行测试CTP4 但在 CTP3 中您将创建一个如下所示的复合密钥:
I haven't tested it with my current database & CTP4 but in CTP3 you'd create a composite key like this: