Tuesday 2 October 2012

EF5.0 Code First - The model backing the 'xxxContext' context has changed since the database was created. Consider using Code First Migrations to update the database

I created a simple bit of code to load data from an existing table into a POCO. However as I was developing the POCO I got this error.

The model backing the 'xxxContext' context has changed since the database was created. Consider using Code First Migrations to update the database

This is because EF creates a table [dbo].[__MigrationHistory] when it is first run and stores the model in there. If you subsequently change the POCO it differs from the saved model and this error is thrown.

My solution was to delete the table and thus any reference to the historical model.

using (var context = new EndurContext())

{

using (var conn = new SqlConnection(connections["EndurContext"]))

{

using (var command = conn.CreateCommand())

{

command.CommandType =
CommandType.StoredProcedure;

command.CommandText = "[trade].[GetPhysFixedDeals]";

conn.Open();

using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))

{

var jobs =

((IObjectContextAdapter) context).ObjectContext.Translate<PhysicalFixedDeal>(reader);

}

}

}

}

No comments:

Post a Comment