简体   繁体   English

将对模型所做的更改提交给数据库

[英]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: 这可以按预期工作,但是随后我对模型进行了更改,并尝试了以下步骤将更改提交到数据库:

  1. I deleted the controller 我删除了控制器
  2. I deleted the View folder containing the various cshtml pages for creating/deleting/etc records 我删除了包含用于创建/删除/ etc记录的各种cshtml页面的View文件夹
  3. I used the server explorer pane in Visual Studio to delete the table from the database 我使用Visual Studio中的服务器资源管理器窗格从数据库中删除表
  4. I recreated the controller based on the model (and by proxy the various views) 我根据模型(并通过代理各种视图)重新创建了控制器
  5. I Saved All 保存了所有
  6. I rebuilt my solution 我重建了解决方案

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: 几个问题:

  1. What is the correct way to commit changes made to a model after the controllers/views/tables have all been created, and potentially have had changes made to them. 在控制器/视图/表全部创建并且可能对其进行更改之后,提交对模型所做的更改的正确方法是什么?
  2. What can I do to fix my current situation? 我该如何解决目前的状况?

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.

相关问题 数据库更新模型恢复对.context文件的手动更改 - Update Model from Database reverts manual changes made to .context file 使用Entity Framework提交对数据库的更改 - Committing changes to the database using Entity Framework 对模型进行更改后如何更新数据库快照以匹配数据库 - How to update the database snapshot to match the database after changes have been made to the model 在datagridview上反映对数据库所做的更改 - Reflect changes made to database on datagridview DirectoryEntry CommitChanges()不提交更改? - DirectoryEntry CommitChanges() not Committing Changes? 是否有一种通用方法可以使EF在提交更改后返回新模型数据和关联的模型数据? - Is there a generic way to get EF to return new model data and associated model data after committing changes? 使用对DataTable所做的更改来更新数据库…混乱 - Update database with changes made to DataTable… confusion 从DataGridView中所做的更改更新数据库 - Update Database from changes made in a DataGridView 自动记录对数据库中记录的更改 - Automatically log changes made to records in database 对数据库进行更改时刷新数据网格 - Refresh datagrid when changes to database are made
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM