Solutions with one EF6 model (db first) and one code first

c# entity-framework entity-framework-6


I have a solution that has multiple databases. We are using EF6.

We have one Project that is EF6 (code first), and one project that is EF6 DB first.

var arkleModelDbFist = new Arkle.DAL.EF.ArkleEntities();
var CustomerOld= new Arkle.DAL.EF.Customer();
arkleModelDbFist.Customers.Add(CustomerOld); // Errors on this line

var arkleModelCodeFirst = new ArkleEfModel();
var customer= new customer_ef(); // note we have renamed this so it won't conflict, even though it is in seperate project

We are getting the following error.

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException was unhandled

Message=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:

StackTrace: at Arkle.DAL.EF.ArkleEntities.OnModelCreating(DbModelBuilder modelBuilder)

Is there anyway to allow both code first and db first in the one solution?

12/18/2014 4:37:20 PM

Accepted Answer

Ok, got to the bottom of this. Basically if you share a connection across both contexts this does not work. However if you use a different connection for both it works.

Thanks to Ben Robinson for pointing me in right direction.

12/23/2014 11:14:18 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow