Question

I am using code-first pattern in entity framework V4.1. One of the things I came across which looks really weird is that whenever you make any changes in the model the database is just dropped and created new. This is very untidy because everytime dropping and recreating the database is not ideal solution. All my test data and everything gets lost. I know there is a method named Seed and you can put test data records to insert there. However there may be few 100s of test records and it's not viable to put every single record in seed method. Is there any other alternative to this? I wouldn't have used code-first at all but it is mandatory to use it in project.

Was it helpful?

Solution

No there is currently no other method and EF itself will probably never support anything more then drop / recreate. The way ADO.NET team chose is using separate tool for database upgrade called Migrations. Migrations are currently available only as CTP = they are not suitable for real usage but for testing and collecting feedback back to EF team.

As a workaround you can have external data population script and call it from seed - it will be much easier for maintenance.

OTHER TIPS

You can set up EF to only attempt to modify your database if it does not already exist.

Database.SetInitializer<YourDataContext>(new CreateDatabaseIfNotExists<YourDataContext>());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top