简体   繁体   中英

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

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
arkleModelDbFist.SaveChanges();


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
arkleModelCodeFirst.Customers.Add(customer);
arkleModelCodeFirst.SaveChanges();

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: http://go.microsoft.com/fwlink/?LinkId=394715

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM