Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code first and initialize multiple contexts

I'm trying out Code First. I've two seperate contexts using the same database.

My problem is how to use the databaseinitializers.

If I use a initializer that drops the database, it only works for the first context. Then I initialize the secont context, and that initializer delets the tables creatde by the first initializer.

Any advices who to solve this?

like image 270
Erik Z Avatar asked Feb 20 '26 13:02

Erik Z


1 Answers

In case of code first you should not have two different context types for the same database. You should have only one - your use case is not expected in code first scenario.

To solve your problem you must simply use initializer only for one context type and make sure that it is always used first.

like image 194
Ladislav Mrnka Avatar answered Feb 23 '26 13:02

Ladislav Mrnka