mithun_daa
mithun_daa

Reputation: 4384

EF Code First different solutions/projects same database

I have 2 different MVC solutions (web apps) that are using Code First. The target for both these apps is the same database. When I started on my second app I initially got some errors about not being able to apply changes to the database (don't remember exactly what they were). I finally managed to just generate the script

update-database -Script

and ran it on the database and it worked. But what i did not realize for about a week is that my other app that uses Code first and talks to the same database but different tables stopped working. When i tried to compile it to debug the issue i got and error which pretty much meant the following: The backing context has changed and these changes need to be pushed to the database (update-database) but when i try to update the database, it fails saying there are no migrations.

Both my apps are MVC3 apps targeting .Net 4.0 in Visual Studio 2012.

Any tips on how to fix this situation?

Upvotes: 1

Views: 1639

Answers (2)

Pawel
Pawel

Reputation: 31620

Unfortunately in EF5 migrations are single-tenant. If you want to use the same database you have to either turn-off migrations and checking whether the database is in-sync with your model or use the same model in both apps. This has been solved in EF6 - here is the speclet for multi-tenant migrations in EF6 http://entityframework.codeplex.com/wikipage?title=Multi-tenant%20Migrations

Upvotes: 2

Steve Danner
Steve Danner

Reputation: 22198

You could refactor the code-first logic from the app that's currently working into a common assembly and reference the new assembly from both projects.

Upvotes: 1

Related Questions