简体   繁体   中英

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

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>());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM