문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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

Database.SetInitializer<YourDataContext>(new CreateDatabaseIfNotExists<YourDataContext>());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top