How to manually update Entity framework Code first model so that it updates a database (with a new column)?

不羁岁月 提交于 2019-12-01 11:52:31

问题


can somebody tell me how I can add a data member (col) to my mvc3 model (class) and have it update the database without having to generate everything from scratch? I'm working from code first. When I change my model then run my project I get an error stating that model has changed. Any clean and easy way to synch creating a new col/data mamber with the db/model?

Thanks!


回答1:


you can use this in application start,

 Database.SetInitializer(new DropCreateDatabaseIfModelChanges<YourDBContext>());

it will regenerate the database if your model change happens.And if you do not want to drop and create database (To incremental development) you can use SqlMigrations. http://www.hanselman.com/blog/EntityFrameworkCodeFirstMigrationsAlphaNuGetPackageOfTheWeek10.aspx



来源:https://stackoverflow.com/questions/7100578/how-to-manually-update-entity-framework-code-first-model-so-that-it-updates-a-da

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!