asp.net mvc:当我在代码中更改模型时,如何使用实体框架自动更新底层数据库?

时间:2022-09-07 15:13:38

I am getting a feel for ASP.NET MVC 4. I have successfully created a simple CRUD application using scaffolding/code generation. I am connected to a DB on sql server via a connection string. Now I have updated the model class that I used to initially create the code. How do I get this to update the model in the underlying project/add the new field in the database?

我对ASP.NET MVC 4有了一种感觉。我已经使用脚手架/代码生成成功创建了一个简单的CRUD应用程序。我通过连接字符串连接到sql server上的DB。现在我已经更新了我最初创建代码的模型类。如何在基础项目中更新模型/在数据库中添加新字段?

I use linq-to-sql all the time for winforms applications so I'm used to changing a table in SQL server and then dragging the modified table from the solution explorer onto the datacontext. What is the analog in asp.net mvc 4? I don't see a datacontext class (in a data folder) in the solution explorer.

我一直对winforms应用程序使用linq-to-sql,所以我习惯在SQL服务器中更改表,然后将修改后的表从解决方案资源管理器拖到datacontext上。 asp.net mvc 4中的模拟是什么?我没有在解决方案资源管理器中看到datacontext类(在数据文件夹中)。

1 个解决方案

#1


1  

Make sure you have the appropriate project selected in the Default Project dropdown in the Package Manager Console

确保在程序包管理器控制台的“默认项目”下拉列表中选择了相应的项目

  • Run the Enable-Migrations command in Package Manager Console
  • 在程序包管理器控制台中运行Enable-Migrations命令
  • Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created
  • Add-Migration将根据您自上次迁移创建以来对模型所做的更改来支持下一次迁移
  • Update-Database will apply any pending migrations to the database
  • Update-Database将应用任何挂起的迁移到数据库

#1


1  

Make sure you have the appropriate project selected in the Default Project dropdown in the Package Manager Console

确保在程序包管理器控制台的“默认项目”下拉列表中选择了相应的项目

  • Run the Enable-Migrations command in Package Manager Console
  • 在程序包管理器控制台中运行Enable-Migrations命令
  • Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created
  • Add-Migration将根据您自上次迁移创建以来对模型所做的更改来支持下一次迁移
  • Update-Database will apply any pending migrations to the database
  • Update-Database将应用任何挂起的迁移到数据库