gpt4 book ai didi

c# - Entity Framework 代码优先 : how to add more props on model and update database?

转载 作者:行者123 更新时间:2023-11-30 21:37:04 24 4
gpt4 key购买 nike

我用 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。我还想创建一个用于审查 CoachReview 类。我的模型类 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 Migrations

In 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com