Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

as all EF creates database,table and column according our class created in front-end automatically in code first approach.

So, i added one more extra coloumn in my class after creating Data base ,table and column.

But, i got the following exception .

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The model backing the 'MvcMusicStoreContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?

this is because of Migration.

so, i added the follow line in Global.asax.cs(Application_Start) to fix that exception.

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<employeedbcontext>());

now works fine.

But the problem my existing database and table values got dropped and creates new as new with empty . i mean there is just column name only available.

is that good in MVC? how can i fix this problem.
Posted

1 solution

The route that you took is an initialization strategy, and it is behaving in the expected manner. You don't want to use it for a production database, what you'll want to use instead is a Migration.

Code First migrations are described in detail at:
http://msdn.microsoft.com/en-us/data/jj591621.aspx[^]

You can also write your own initialize, but I would not go down that route unless you want to start getting into the weeds of how EF interacts with the database.

The most important lesson here: migrations != initialization strategy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900