我有一个奇怪的问题,我找不到解决方案。
堆栈是:NET Core 2、EF、PostgreSQL。
我将 .NET Core Identity 与 User : IdentityUser
结合使用,以使用其他字段扩展基本用户模型。
在我创建第一次迁移后,删除整个数据库并尝试 dotnet ef 数据库更新
我总是收到错误:42P07: relation "AspNetRoles"already exists
即使有这个错误,数据库和表也被创建了,但它使迁移变得无用,因为它不保存应用的迁移,所以我无法用以下更改更新数据库......
fail: Microsoft.EntityFrameworkCore.Database.Command[200102] Failed executing DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE "AspNetRoles" ( "Id" text NOT NULL, "ConcurrencyStamp" text NULL, "Name" varchar(256) NULL, "NormalizedName" varchar(256) NULL, CONSTRAINT "PK_AspNetRoles" PRIMARY KEY ("Id") ); Npgsql.PostgresException (0x80004005): 42P07: relation "AspNetRoles" already exists at Npgsql.NpgsqlConnector.d__148.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult() at System.Runtime.CompilerServices.ValueTaskAwaiter
1.GetResult()
at Npgsql.NpgsqlConnector.d__147.MoveNext()
最佳答案
原因很简单。
我在 Startup.cs
中调用了 EnsureCreated
,由于工作方式不同,它与迁移发生冲突。
感谢 EF Core 所有者在 GitHub 上为我明确说明。
总结一下——如果你想使用 Migrations,你不能使用 EnsureCreated。
关于c# - 无法在 EF Core : "42P07: relation "AspNetRoles"already exists"中使用迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47420370/