Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i add a table in my existing database without losing data in Entity-framework Code first 5.0 ?

Recently I updated my software and I need to add a new table in my database. I am using

Update-Database -Force -Verbose

to update my database in my development PC. But when I install update version of my software in my client machine then it throw following exception.

The model backing the ‘MyContext’ context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)

If I drop and recreate database then it`s work fine.

But it is quite impossible to drop existing database in my client machine. I have to add my new table without losing any data.

Please tell me how can i add a new table without out losing data in my existing Database.

I am using

Visual Studio 2012
Entity Framework 5.0
DotNet 4.0
MSSQL Server 2008 R2
like image 569
Hasanuzzaman Avatar asked Oct 05 '22 20:10

Hasanuzzaman


1 Answers

You need to use Code First Migrations. When you set up your DbContext it will look something like this:

Database.SetInitializer<dbContext>(new MigrateDatabaseToLatestVersion<dbContext, Configuration>());
like image 55
Robert Avatar answered Oct 12 '22 21:10

Robert