[英]Committing changes made to a model to the database
I had built a model which I made a controller (and by proxy a number of views for viewing, creating, deleting and editing records in the table created whenever I first browsed to the view via the controller in my web browser) out of using the wizard in Visual Studio. 我建立了一个模型,该模型使控制器成为了控制器(并且通过代理,使多个视图可以在我第一次通过Web浏览器中的控制器浏览到视图时在创建的表中查看,创建,删除和编辑记录),而不使用Visual Studio中的向导。
This worked as expected, but then I made a change to my model, and tried the following steps to commit the changes to the database: 这可以按预期工作,但是随后我对模型进行了更改,并尝试了以下步骤将更改提交到数据库:
After I carried out the above, I hit debug, and browsed to my controller, expecting the entity framework to re-create my table. 完成上述操作后,我单击了debug,然后浏览到控制器,期望实体框架重新创建我的表。 Instead I got this error: 相反,我得到了这个错误:
Invalid object name 'dbo.FooBar'. 无效的对象名称“ dbo.FooBar”。 // FooBar is the name of my model, and therefore my table // FooBar是我的模型的名称,因此是我的表的名称
A couple of questions: 几个问题:
Thanks 谢谢
The two packages you want to use would be: 您要使用的两个软件包是:
Automatic Migrations (EFMigrations) 自动迁移(EFMigrations)
http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx
This Feature extends your Project with the ability to not only use code first, but also migrates changes made to your model to the database. 此功能扩展了您的项目,使其不仅可以首先使用代码,还可以将对模型所做的更改迁移到数据库。 I am using this in production since the minute it was out of beta. 我已经在生产中使用它,因为它已经超出beta了。
MVC Scaffolding MVC脚手架
http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/ http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/
You can either download existing scaffolding templates to create views based on your model. 您可以下载现有的脚手架模板以根据模型创建视图。 These views are then automaticly updated if your model changes. 如果您的模型发生更改,这些视图将自动更新。 Never got around to build my own templates, but heres a blog post from Scott Hanselman who Shows how to do that: http://www.hanselman.com/blog/ModifyingTheDefaultCodeGenerationscaffoldingTemplatesInASPNETMVC.aspx 从来没有到过构建自己的模板的过程,而是来自Scott Hanselman的博客文章,该博客文章演示了如何做到这一点: http : //www.hanselman.com/blog/ModifyingTheDefaultCodeGenerationscaffoldingTemplatesInASPNETMVC.aspx
Check out the talks from Steve Sanderson (Scaffolding) and Scott Hanselman (EF Migrations) on Channel9. 在Channel9上查看Steve Sanderson(Scaffolding)和Scott Hanselman(EF Migrations)的演讲。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.