我使用 Visual Studio 2017 创建了一个新的 ASP.NET Core 2.0 项目。目前我有一个 Coach
模型类:
public class Coach
{
public int ID { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public string Methodology { get; set; }
public string Price { get; set; }
public int Rating { get; set; }
//New property
public string Email { get; set; }
}
我想向 Coach
类添加一个新属性 Email
。我还想创建一个用于审查Coach
的Review
类。我的模型类 User
评论教练。但是,当我添加新属性和新类时,我会在 Web 应用程序启动时收到此消息:
A database operation failed while processing the request.
SqlException: Invalid column name 'Email'.
Applying existing migrations for ApplicationDbContext may resolve this issue. There are migrations for ApplicationDbContext that have not been applied to the database
00000000000000_CreateIdentitySchema
Apply MigrationsIn Visual Studio, you can use the Package Manager Console to apply pending migrations to the database:
PM> Update-Database
Alternatively, you can apply pending migrations from a command prompt at your project directory:dotnet ef database update
按照我收到的消息操作不起作用。问题仍然存在。谁能指出我正确的方向如何更新代码,然后更新本地存储的数据库?当首先更改模型代码并希望数据库反射(reflect)该更改时,工作流程应该是什么样的?
最佳答案
生成并运行迁移
Code First Migrations 有两个您需要运行的主要命令。
Add-Migration
将根据自上次迁移创建以来对模型所做的更改来构建下一次迁移
Update-Database
将把所有挂起的迁移应用到数据库
我们需要构建迁移来处理我们添加的新 Email
属性。 Add-Migration
命令允许我们为这些迁移命名,我们将其命名为 AddEmail
。
在程序包管理器控制台中运行Add-Migration AddEmail
命令
在包管理器控制台中运行Update-Database
命令
关于c# - Entity Framework 代码优先 : how to add more props on model and update database?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47617326/