gpt4 book ai didi

c# - ASP.net core 2.0 MVC 每个新的数据库迁移都包含以前迁移的数据

转载 作者:搜寻专家 更新时间:2023-10-30 20:49:25 26 4
gpt4 key购买 nike

您好,我在添加新模型或编辑旧模型时尝试进行数据库迁移时遇到了问题。我正在使用 EntityFrameworkCore。问题是每个新迁移都包含前一个迁移的 Up 方法的代码。

/* migrationBuilder.DropIndex( name: "用户名索引", 表:“AspNetUsers”);

        migrationBuilder.DropIndex(
name: "IX_AspNetUserRoles_UserId",
table: "AspNetUserRoles");

migrationBuilder.DropIndex(
name: "RoleNameIndex",
table: "AspNetRoles");

migrationBuilder.AddColumn<DateTime>(
name: "Birthdate",
table: "AspNetUsers",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

migrationBuilder.AddColumn<string>(
name: "FirstName",
table: "AspNetUsers",
nullable: true);

migrationBuilder.AddColumn<string>(
name: "LastName",
table: "AspNetUsers",
nullable: true);
*/

migrationBuilder.CreateTable(
name: "Articles",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
AuthorId = table.Column<string>(nullable: true),
Content = table.Column<string>(maxLength: 25000, nullable: false),
PublishDate = table.Column<DateTime>(nullable: false),
Title = table.Column<string>(maxLength: 150, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Articles", x => x.Id);
table.ForeignKey(
name: "FK_Articles_AspNetUsers_AuthorId",
column: x => x.AuthorId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});

在这里,我试图将一个新模型文章迁移到数据库,但该方法包含以前迁移的一些内容,当我尝试更新数据库时,出现以下错误:“无法删除索引‘AspNetUserRoles.IX_AspNetUserRoles_UserId’,因为它不存在或您没有权限。”如果我评论之前迁移的代码并只保留适当模型的数据,它就会成功迁移。但后来我添加了一个新模型“Supplement”,并通过在 Up 方法中添加的 Article 迁移得到了同样的结果:

/* migrationBuilder.DropIndex( name: "用户名索引", 表:“AspNetUsers”);

        migrationBuilder.DropIndex(
name: "IX_AspNetUserRoles_UserId",
table: "AspNetUserRoles");

migrationBuilder.DropIndex(
name: "RoleNameIndex",
table: "AspNetRoles");

migrationBuilder.AddColumn<DateTime>(
name: "Birthdate",
table: "AspNetUsers",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

migrationBuilder.AddColumn<string>(
name: "FirstName",
table: "AspNetUsers",
nullable: true);

migrationBuilder.AddColumn<string>(
name: "LastName",
table: "AspNetUsers",
nullable: true);
*/

migrationBuilder.CreateTable(
name: "Articles",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
AuthorId = table.Column<string>(nullable: true),
Content = table.Column<string>(maxLength: 25000, nullable: false),
PublishDate = table.Column<DateTime>(nullable: false),
Title = table.Column<string>(maxLength: 150, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Articles", x => x.Id);
table.ForeignKey(
name: "FK_Articles_AspNetUsers_AuthorId",
column: x => x.AuthorId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});

migrationBuilder.CreateTable(
name: "Supplements",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Description = table.Column<string>(nullable: true),
Name = table.Column<string>(nullable: true),
Price = table.Column<decimal>(nullable: false),
Quantity = table.Column<int>(nullable: false),
SupplementType = table.Column<int>(nullable: false),
inStock = table.Column<bool>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Supplements", x => x.Id);
});

我该如何解决这个问题?

最佳答案

另一种方法是确保映射正确,只删除上/下函数以获得干净的状态:

https://github.com/aspnet/EntityFrameworkCore/issues/11763

关于c# - ASP.net core 2.0 MVC 每个新的数据库迁移都包含以前迁移的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47708885/

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