How to create a composite primary key in Entity Framework 4 code first?

First I get the EF4 code and I like it. But itโ€™s hard for me to match an entity with a table with a composite primary key.

The elaborated configuration is as follows:

public SubscriptionUserConfiguration() { Property(u => u.SubscriptionID).IsIdentity(); Property(u => u.UserName).IsIdentity(); } 

What causes this exception: Failed to infer the key for the entity type 'SubscriptionUser'.

What am I missing?

+42
orm entity-framework composite-key
Apr 28 '10 at 9:26 a.m.
source share
4 answers

You can also use

 HasKey(u => new { u.SubscriptionID, u.UserName }); 

Edit:

One limitation that I discovered is this:

 public ProjectAssignmentConfiguration() { HasKey(u => u.Employee.EmployeeId); HasKey(u => u.Project.ProjectId); } 

or

 public ProjectAssignmentConfiguration() { HasKey(u => new { u.Employee.EmployeeId, u.Project.ProjectId }); } 

So, how do you set up an entity where in the connection table there is a primary key consisting of foreign keys?

+67
May 26 '10 at 12:29
source share

I will try to explain this step by step using the following Entity

 public class Account { public int AccountId1 { get; set; } public int AccountId2 { get; set; } public string Description { get; set; } } 
  • Create a class derived from EntityTypeConfiguaration<TEntity> to override conventions

     class AccountEntityTypeConfiguration : EntityTypeConfiguration<Account> { public AccountEntityTypeConfiguration() { // The Key // The description of the HasKey Method says // A lambda expression representing the property to be used as the primary key. // If the primary key is made up of multiple properties then specify an anonymous type including the properties. // Example C#: k => new { k.Id1, k.Id2 } // Example VB: Function(k) New From { k.Id1, k.Id2 } this.HasKey(k => new { k.AccountId1, k.AccountId2 } ); // The Key // Maybe the key properties are not sequenced and you want to override the conventions this.Property(p => p.AccountId1).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None); this.Property(p => p.AccountId2).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None); this.Property(p => p.Description).IsRequired(); // This property will be required this.ToTable("Account"); // Map the entity to the table Account on the database } } 
  • Create a class derived from the DbContext Object, override OnModelCreating and add a new AccountEntityTypeConfiguration to the model builder configuration.

     public class MyModelAccount : DbContext { public DbSet<Account> Accounts { get; set;} protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Add a new AccountEntityTypeConfiguration object to the configuration of the model, that will be applied once the model is created. modelBuilder.Configurations.Add(new AccountEntityTypeConfiguration()); } } 

Hope this helps you!

+19
Nov 17 '11 at 11:19
source share

You can also use the Column attribute

 public class UserProfileRole { [Key, Column(Order = 0)] public int UserId { get; set; } [Key, Column(Order = 1)] public int RoleId { get; set; } } 
+13
Aug 07 '13 at 22:56
source share

Decided: I should use HasKey, not Identity. It works:

 public SubscriptionUserConfiguration() { HasKey(u => u.SubscriptionID); HasKey(u => u.UserName); } 
+5
Apr 29 '10 at 7:47 april
source share



All Articles