Use some tables from another databese: The model backing the 'ApplicationDbContext' context has changed since the database was created RRS feed

  • Question

  • I have a database named "mydb" that has identity tables. Now, I need my project uses the identity tables that are already exist on another "mydb2" database, and uses all the other tables from "mydb" .

    So the project will use the identity tables of the "mydb2" database and the other tables from the "mydb" database.

    To do this, I have just updated my connection strings like this

    <add name="DB2Entities" connectionString="Data Source=***;InitialCatalog=mydb2;User ID=user;Password=password;" providerName="System.Data.SqlClient" />
    <add name="DBEntities" connectionString="metadata=res://*/ApiDb.csdl|res://*/ApiDb.ssdl|res://*/ApiDb.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=***;initial catalog=mydb;persist security info=True;user id=user;password=password;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

    But in execution I get the error: The model backing the 'ApplicationDbContext' context has changed since the database was created. This could have happened because the model used by ASP.NET Identity Framework has changed or the model being used in your application has changed. To resolve this issue, you need to update your database. Consider using Code First Migrations to update the database (  Before you update your database using Code First Migrations, please disable the schema consistency check for ASP.NET Identity by setting throwIfV1Schema = false in the constructor of your ApplicationDbContext in your application.
           public ApplicationDbContext() : base("ApplicationServices", throwIfV1Schema:false)

    I have also tried setting the initializer to null

    public AuthAppContext()
      : base("name=DB2Entities")

    but I have the same error.

    Note: I do not mind losing the data that are currently in the identity of "mydb" database. I just want to use the ones that are into mydb2.

    Friday, January 30, 2015 11:42 AM

All replies

  • Hello,

    From your description, my understanding is that you want to use some tables which does not belong to the model in your project. If it is, unfortunately, current Entity Framework does not support two or more different databases at runtime, one DbContext is mapped to one database at the same when application is started. It would detect the code first model and stored model in database and compare them, if there are difference and you do not provide a proper process, it would throw this exception which notifies you to use migration to update the database.

    My suggestion is that you could create a separate dbcontext class for your requirement.

    I notice that you are working with the ASP.NET Identity component, I suggest that you could also ask it to the forum to see if experts have a workaround for your requirement. From the Entity Framework, it is not possible.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 2, 2015 5:50 AM