UnintentionalCodeFirstException | Entity Framework Unit Testing with Effort.Ef6 using Database First


The situation

I want to enable unit testing of my DbContext based on entity framework 6. I have created my DbContext and my models with the database first approach and now have an .edmx designer file.

The problem

My automatically created DbContext does override the DbContext.OnModelCreating method like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    throw new UnintentionalCodeFirstException();

I am trying to create a new instance of my DbContext in my mocked data access service based on the information of this article. My code doesn't run into Database.SetInitializer; like the author of this blog post pointed out. My constructor looks exactly like his. My DbContext initialization looks like this:

public DatabaseEntities(DbConnection connection) 
     : base(connection, true) { }

Which results in following exception when reaching the previously mentioned overridden OnModelCreating

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: 'The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715'

There are plenty of questions on stackoverflow which focus on unit testing with effort while using the database-first apporach, but no one seems to have similar problems like me.

What I have already tried to do

  • Searching the internet a long time for a solution.
  • Uncomment the overridden OnModelCreating().
  • I have already tried to apply this solution without success.
  • Mock my DbContext with Moq, which is definitely not an option.

The question

How can i create and use an in-memory database (with effort)?

Additional information

When I uncomment the OnModelCreating(), an exception will be thrown at the first access to the DataContext. E.g.:

DbConnection effortConnection = Effort.DbConnectionFactory.CreatePersistent("MockedDatabaseEntities");

using (DatabaseEntities dataContext = new DatabaseEntities(effortConnection))
    dataContext.Students.Count(); // Exception throws here

System.Data.Entity.ModelConfiguration.ModelValidationException: 'One or more validation errors were detected during model generation: MyApplication.Shared.Model.KeyValuePair: : EntityType 'KeyValuePair' has no key defined. Define the key for this EntityType. KeyValuePairs: EntityType: EntitySet 'KeyValuePairs' is based on type 'KeyValuePair' that has no keys defined.'

Accepted Answer

The problem was that I had to specify the key for my KeyValuePair dataset.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    modelBuilder.Entity<KeyValuePair>().HasKey(x => x.Key);
    base.OnModelCreating(modelBuilder, null);

Thank you very much Robert Jørgensgaard Engdahl !

Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why