c# - Moving Entity framework to another project from MVC causes re-migration -


i have asp.net mvc 4 application contains entity framework 6 code first models, dbcontext , migrations. in attempt separate web application can re-use these database classes in project have moved related entity framework classes own project.

however when run solution thinks model has changed , attempts run migrations once more. problem appears in use of setinitializer if comment out line can run web application per normal.

public static class databaseconfig {     public static void initialize()     {        system.data.entity.database.setinitializer(new migratedatabasetolatestversion<g5datacontext, configuration>());          // make sure database created before simplemembership initialised         using (var db = new g5datacontext())              db.database.initialize(true);     } } 

this wasn't problem until i've tried move entity framework classes. not possible, or have done fundamentally wrong?

at startup, ef6 queries exiting migrations in database, stored in __migrationhistory table. part of table context key, includes namespace of entities.

if move new namespace, ef6 doesn't recognize of run migrations, , tries rebuild database.

a quick solution run script rename context key in __migrationhistory table new namespace. http://jameschambers.com/2014/02/changing-the-namespace-with-entity-framework-6-0-code-first-databases/ :

update [dbo].[__migrationhistory]     set [contextkey] = 'new_namespace.migrations.configuration'  [contextkey] = 'old_namespace.migrations.configuration' 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -