英文:
__EFMigrationsHistory remains empty even though the database is created
问题
I have translated the provided text:
嗨,我在EF Core 6中遇到了一个问题,即迁移的历史表仍然为空,但是当我检查SQL Server时,数据库已成功创建并且所有表也都创建了。
我使用了Code-First方法,所以我运行Add-Migration <name>
来生成迁移。
当我运行Update-Database
时,一切都正常运行。
我所做的是将EF Core从6.0.16
升级到6.0.21
,然后在我的模型中添加了一个新的实体mytablename
,所以我生成了它的迁移。然后我运行Update-Database
,现在它给了我这个错误:
Microsoft.Data.SqlClient.SqlException (0x80131904): 数据库中已存在一个名为'AspNetRoles'的对象。错误编号:2714,状态:6,类:16
mytablename
已创建,但__EFMigrationsHistory
未更新。
我尝试了一些解决方案,比如删除数据库并从头开始重新创建,然后尝试删除数据库和所有迁移并重新生成它们,但仍然没有解决问题,仍然出现相同的错误。
谢谢您帮助我,我是EF Core和DotNet产品的新手。
我尝试了一些解决方案,比如删除数据库并从头开始重新创建,然后尝试删除数据库和所有迁移并重新生成它们,但仍然没有解决问题,仍然出现相同的错误。
谢谢您帮助我,我是EF Core和DotNet产品的新手。
以下是第一个迁移:
public partial class CreateIdentitySchema : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "AspNetRoles",
columns: table => new
{
Id = table.Column<string>(nullable: false),
Name = table.Column<string>(maxLength: 256, nullable: true),
NormalizedName = table.Column<string>(maxLength: 256, nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetRoles", x => x.Id);
});
// 其他表的创建也在此处
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "AspNetRoleClaims");
// 其他表的删除也在此处
}
}
当我手动填充__EFMigrationsHistory
并运行update-database
时,一切正常,但当我删除数据库并重新运行update-database
时,会出现相同的错误。
当我运行dotnet ef migrations list
时:
> 00000000000000_CreateIdentitySchema (待处理)
>
> 20230807142821_CreateAppDb (待处理)
>
> 20230809091829_AppTable (待处理)
>
> 20230809100343_UserPersonalData (待处理)
>
> 20230810084347_db (待处理)
>
>
> 程序:
>
public static class Program
{
public static async Task Main(string[] args)
{
// 程序配置在此处
}
}
>
> DbContext
>
public class ApplicationDbContext :
IdentityDbContext<ApplicationUser>
{
// DbContext配置在此处
}
请注意,代码中的"
应该是"
,我已经做了修正。
英文:
Hi I have an issue with EF Core 6 that is the history table of migrations remains empty but when I check the sql server the db is created successfully with all the tables are created also.
I use Code-first approach so I run Add-Migration <name>
to generate migrations.
When I run Update-Database
I was running perfectly fine.
what I did is updating EF Core from 6.0.16
to 6.0.21
, Added new Entity mytablename
to my models so I
generate its migration. then I run Update-Database
it gives me now
Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'AspNetRoles' in the database. Error Number:2714,State:6,Class:16
mytablename
is created but __EFMigrationsHistory
is not updated.
I tried some solutions like drop db and recreated from scratch, then I tried deleting db and all migrations and regenerate them but it did not work still same error.
thank you for helping me I am new to EF Core and DotNet products.
I tried some solutions like dropping db and recreated it from scratch, then I tried deleting db and all migrations and regenerate them but it did not work still same error.
thank you for helping me I am new to EF Core and DotNet products.
here is the first migration:
public partial class CreateIdentitySchema : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "AspNetRoles",
columns: table => new
{
Id = table.Column<string>(nullable: false),
Name = table.Column<string>(maxLength: 256, nullable: true),
NormalizedName = table.Column<string>(maxLength: 256, nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetRoles", x => x.Id);
});
migrationBuilder.CreateTable(
name: "AspNetUsers",
columns: table => new
{
Id = table.Column<string>(nullable: false),
UserName = table.Column<string>(maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
Email = table.Column<string>(maxLength: 256, nullable: true),
NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
PasswordHash = table.Column<string>(nullable: true),
SecurityStamp = table.Column<string>(nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true),
PhoneNumber = table.Column<string>(nullable: true),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
TwoFactorEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
LockoutEnabled = table.Column<bool>(nullable: false),
AccessFailedCount = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUsers", x => x.Id);
});
migrationBuilder.CreateTable(
name: "AspNetRoleClaims",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
RoleId = table.Column<string>(nullable: false),
ClaimType = table.Column<string>(nullable: true),
ClaimValue = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id);
table.ForeignKey(
name: "FK_AspNetRoleClaims_AspNetRoles_RoleId",
column: x => x.RoleId,
principalTable: "AspNetRoles",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "AspNetUserClaims",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
UserId = table.Column<string>(nullable: false),
ClaimType = table.Column<string>(nullable: true),
ClaimValue = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUserClaims", x => x.Id);
table.ForeignKey(
name: "FK_AspNetUserClaims_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "AspNetUserLogins",
columns: table => new
{
LoginProvider = table.Column<string>(maxLength: 128, nullable: false),
ProviderKey = table.Column<string>(maxLength: 128, nullable: false),
ProviderDisplayName = table.Column<string>(nullable: true),
UserId = table.Column<string>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey });
table.ForeignKey(
name: "FK_AspNetUserLogins_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "AspNetUserRoles",
columns: table => new
{
UserId = table.Column<string>(nullable: false),
RoleId = table.Column<string>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId });
table.ForeignKey(
name: "FK_AspNetUserRoles_AspNetRoles_RoleId",
column: x => x.RoleId,
principalTable: "AspNetRoles",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_AspNetUserRoles_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "AspNetUserTokens",
columns: table => new
{
UserId = table.Column<string>(nullable: false),
LoginProvider = table.Column<string>(maxLength: 128, nullable: false),
Name = table.Column<string>(maxLength: 128, nullable: false),
Value = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name });
table.ForeignKey(
name: "FK_AspNetUserTokens_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_AspNetRoleClaims_RoleId",
table: "AspNetRoleClaims",
column: "RoleId");
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
table: "AspNetRoles",
column: "NormalizedName",
unique: true,
filter: "[NormalizedName] IS NOT NULL");
migrationBuilder.CreateIndex(
name: "IX_AspNetUserClaims_UserId",
table: "AspNetUserClaims",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_AspNetUserLogins_UserId",
table: "AspNetUserLogins",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_AspNetUserRoles_RoleId",
table: "AspNetUserRoles",
column: "RoleId");
migrationBuilder.CreateIndex(
name: "EmailIndex",
table: "AspNetUsers",
column: "NormalizedEmail");
migrationBuilder.CreateIndex(
name: "UserNameIndex",
table: "AspNetUsers",
column: "NormalizedUserName",
unique: true,
filter: "[NormalizedUserName] IS NOT NULL");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "AspNetRoleClaims");
migrationBuilder.DropTable(
name: "AspNetUserClaims");
migrationBuilder.DropTable(
name: "AspNetUserLogins");
migrationBuilder.DropTable(
name: "AspNetUserRoles");
migrationBuilder.DropTable(
name: "AspNetUserTokens");
migrationBuilder.DropTable(
name: "AspNetRoles");
migrationBuilder.DropTable(
name: "AspNetUsers");
}
}
When I populate __EFMigrationsHistory manually and run update-database
it works fine , but when I drop the db and rerun update-database
it gives the same error.
When I run dotnet ef migrations list
:
> 00000000000000_CreateIdentitySchema (Pending)
>
> 20230807142821_CreateAppDb (Pending)
>
> 20230809091829_AppTable (Pending)
>
> 20230809100343_UserPersonalData (Pending)
>
> 20230810084347_db (Pending)
>
>
> Program:
>
public static class Program
{
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
//authorization to apps policies
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("DelibAppAccess", policy =>
policy.RequireClaim("AllowedToAppName"));
});
builder.Services.AddScoped<IApplicationService, ApplicationService>();
builder.Services.AddScoped<IRoleService, RoleService>();
builder.Services.AddScoped<IRegisterService, RegisterService>();
//identity password
builder.Services.Configure<IdentityOptions>(options =>
{
// Default Password settings.
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 0;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
using (var scope = app.Services.CreateScope())
{
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var roles = new[] { "SuperAdmin", "Admin", "Member", "Guest" };
foreach (var role in roles)
{
if (!await roleManager.RoleExistsAsync(role))
await roleManager.CreateAsync(new IdentityRole(role));
}
var dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>();
var apps = new[] { "AppName" };
if (dbContext is not null)
{
foreach (var application in apps)
{
if (!dbContext.Applications!.Where(a => a.Label.Equals(application)).Any())
{
dbContext.Applications!.Add(new Application() { Label = application });
}
}
await dbContext.SaveChangesAsync();
}
}
app.Run();
}
}
>
> DbContext
>
public class ApplicationDbContext :
IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
{
}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
try
{
if (Database.GetService<IDatabaseCreator>() is RelationalDatabaseCreator databaseCreator)
{
if (!databaseCreator.CanConnect())
{
databaseCreator.CreateAsync().Wait();
}
if (!databaseCreator.HasTables())
{
databaseCreator.CreateTablesAsync().Wait();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//filtering users by isEnabled
modelBuilder.Entity<ApplicationUser>().HasQueryFilter(user => user.IsEnabled);
base.OnModelCreating(modelBuilder);
}
public DbSet<Application>? Applications { get; set; }
}
答案1
得分: 0
我通过移除以下代码解决了这个问题:
try
{
if (Database.GetService<IDatabaseCreator>() is RelationalDatabaseCreator databaseCreator)
{
if (!databaseCreator.CanConnect())
{
databaseCreator.CreateAsync().Wait();
}
if (!databaseCreator.HasTables())
{
databaseCreator.CreateTablesAsync().Wait();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
英文:
I solved the problem by removing:
try
{
if (Database.GetService<IDatabaseCreator>() is RelationalDatabaseCreator databaseCreator)
{
if (!databaseCreator.CanConnect())
{
databaseCreator.CreateAsync().Wait();
}
if (!databaseCreator.HasTables())
{
databaseCreator.CreateTablesAsync().Wait();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论