user395087
user395087

Reputation: 11

Alter Database in Entity

hello I want to alter my database structure according to my entity edmx file? I know how to create a database:

if(myEntity.DatabaseExists())
{
   //?????
   //check database if there is any change in database alter database according to edmx file.    
}
else
{
    myEntity.CreateDatabase();
}

why do i want to do this?I want install a new version of my program.I use entity and my edmx file has been changed so my database has to change according my new edmx file.

Upvotes: 1

Views: 231

Answers (1)

Steven
Steven

Reputation: 172835

Entity Framework does not support this. Reason for this 'feature' to be missing is because it isn't trivial to do so. Do you want EF to alter your production database and possibly remove columns with data? I don't think so.

What you need to do is create scripts that allow you to update the database schema. If you want to do this automatically, you can add some sort of 'Version' table to your database that holds the version number of the schema. On startup you can check the version number and run all schema update scripts for newer versions (within a transaction).

I always use Microsoft SQL Profiler to record the changes I make to my schema using the SQL Server Management Studio. You could also use tool like SQL Compare (from RedGate) to compare two database and generate the scripts for the changes.

EF cannot run updates for you automatically, you need to run those database scripts yourself.

Upvotes: 1

Related Questions