It turns out that I want to perform a migration but I don't want to lose the data in the database and when trying to update my database I receive the following error:
It should be said that I am using asp.net core 3.1
CodePudding user response:
There is already an object named 'AspNetRoles' in the database.
As the issue message said, when you update the database via the EF core, there is already an object named 'AspNetRoles' in the database.
To solve this issue, you can comment the content of Up method in the migration where all authorization tables are created. Like this:
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.
}
}
Besides, if you meet this issue when using the Fluent API Configurations, you could try to add base.OnModelCreating(builder); in the OnModelCreating method, like this (change the ApplicationDbContext to yours):
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
}
}

