Trouble with Keys

I’ve just spent a couple of hours scratching my head with database/entity framework problem.

My client has a requirement to duplicate records from one database to another when the first is updated.

The tables were identical and based on a class inherited from the IdentityUserRole class, with another field added to the composite key.  However, although data saved OK into the original database, an error was being thrown when trying to duplicate the records into the second database.

After some debugging, I found out that it was an issue with the primary key. It wasn’t recognising the composite key.

The quick fix was to add the following into the dbContext class, in the OnModelCreating method:

 .HasKey(x=>new{x.RoleId,x.UserId, x.TeamId});

Everything immediately started working.

Entity Framework and recreating the database when the model changes.

I’ve just started a new MVC5 project, and just couldn’t remember exactly how to do this.


  1. Create a new project
  2. Create your models
  3. Create your DbContext
  4. Create a Database Initializer
  5. Add Database Initializer into Global.asax
public class ApplicationDbContextInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
    protected override void Seed(ApplicationDbContext context)
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbContextInitializer());


Entity Framework v6 – Multiple Migrations

I love the that EF6 now allows managing migrations for multiple data contexts. This is great as it allows segregation of data tables into their own ‘domains’.

If we have a website called VideoStore, and two data contexts, one for user account details called UserAccountContext and one for a more business specific domain called VideosContext, then you can create migrations for both even if they are using the same database.

First both contexts will need a connection string in the web.config, the connections strings can point to the same database though.

Next, to enable migrations we need to run the enable-migrations command in the package manager console, and we need to include the new -ContextTypeName parameter. By default this would create the migrations directory of the root of the project, however since we have multiple we want to add the –MigrationsDirectory parameter to specify the directory for each context.

enable-migrations -ContextTypeName  UserAccountContext -MigrationsDirectory DataContexts\UserAccountMigrations
enable-migrations -ContextTypeName VideoContext -MigrationsDirectory DataContexts\VideoMigrations
Edit the configurations created and turn off automatic migrations.

Then to add a migration for the context,

add-migration -ConfigurationTypeName VideoStore.Web.DataContexts.UserAccountMigrations.Configuration "InitialCreate"
add-migration -ConfigurationTypeName VideoStore.Web.DataContexts.VideoMigrations.Configuration "InitialCreate"

Then we want to create the database:

update-database -ConfigurationTypeName VideoStore.Web.DataContexts.UserAccountMigrations.Configuration
update-database -ConfigurationTypeName VideoStore.Web.DataContexts.VideoMigrations.Configuration 

And that’s it. Super Easy.