简体   繁体   中英

MVC4 Entity Framework and Seed confusion

I'm hoping someone can help me, I created a Code First EF MVC4 project. With this I used the Seed method and a DbInitializer using DropCreateDatabaseAlways to insert data into the database. However according to the Asp.Net website you must remove all code that seeds the database when publishing.

My question is how can I add data to the database (via the same process as can in Seed method) if I can't use the Seed method without it being added every single time ie as a one off. Have I no alternative but to add to the database via Database Explorer or SQL Server Management Studio?

Also is it worth while to create a 2nd DbInitializer so I have one for test and one for release as I don't want to delete all the seed code?

Our approach to this is to connect your dev environment to the empty live database, run the create and seed methods, then edit your code to remove them, before you push to the app server, and reduce the database rights appropriately.

If you can't do this because your DBAs won't allow it the other approach would be to create a virgin database complete with seeded data on the dev server and then get the DBA's to backup and restore the database to the live environment.

Essentially, yes it's not a good idea to leave the create and seed code in your live app, but there are still ways in which you can use the code first methodology to make and seed your live database.

I would also look into using migrations. These provide all the same functionality as the code first method but with more granular control and less permissions required.

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