Entity Framework CTP4 and Compound Keys

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

I created 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 request "MyEntities" for all of my posts:

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

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

I suspect that the problem is related to the composite key, since no other entity shows this problem.

Any help would be appreciated, thanks :)

+4
c # composite-key entity-framework-4
Jul 21 2018-10-21T00:
source share
1 answer

I have not tested it with my current database and CTP4, but in CTP3 you would create such a composite key:

 HasKey(m => new { m.Key1, m.Key2 }); 
+6
Jul 21 '10 at 13:03
source share



All Articles