事實證明,我想執行遷移,但我不想丟失資料庫中的資料,并且在嘗試更新我的資料庫時收到以下錯誤:
應該說我用的是asp.net core 3.1

uj5u.com熱心網友回復:
資料庫中已經有一個名為“AspNetRoles”的物件。
正如問題訊息所說,當您通過 EF 核心更新資料庫時,資料庫中已經有一個名為“AspNetRoles”的物件。
為了解決這個問題,您可以在創建所有授權表的遷移中注釋 Up 方法的內容。像這樣:
public partial class initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
//migrationBuilder.CreateTable(
// name: "AspNetRoles",
// columns: table => new
// {
// Id = table.Column<string>(nullable: false),
// ConcurrencyStamp = table.Column<string>(nullable: true),
// Name = table.Column<string>(maxLength: 256, nullable: true),
// NormalizedName = table.Column<string>(maxLength: 256, nullable: true)
// },
// constraints: table =>
// {
// table.PrimaryKey("PK_AspNetRoles", x => x.Id);
// });
//
// ... Other Identity tables, sucn as AspNetUsers, AspNetUserRoles.
}
}
此外,如果您在使用 Fluent API 配置時遇到此問題,您可以嘗試添加base.OnModelCreating(builder);OnModelCreating 方法,如下所示(更改ApplicationDbContext為您的):
public class ApplicationDbContext : IdentityDbContext
{
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
//Fluent API Configurations
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/410247.html
標籤:
