c# - 无法在 EF Core : "42P07: relation "AspNetRoles"already exists"中使用迁移

标签 c# postgresql entity-framework asp.net-core

我有一个奇怪的问题,我找不到解决方案。

堆栈是: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.TaskAwaiter1.GetResult() at System.Runtime.CompilerServices.ValueTaskAwaiter1.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/

相关文章:

postgresql - TypeORM postgresql 急切加载关系定义为惰性

.net - 您使用 Entity Framework 的经历如何?

c# - 使用 Selenium 使用 WindowHandles 跟踪和遍历选项卡和窗口的最佳方法

c# - 将 Azure Functions 与事件中心集成

sql - Postgresql 选择一系列由给定间隔隔开的记录

c# - 模拟新的 Microsoft Entity Framework Identity UserManager 和 RoleManager

entity-framework - 如何防止Json.NET使用 Entity Framework 代理类型名称?

c# - 如何在 WPF 中使用左右键在 tabitem 之间导航

c# - 如何在通信图中表示循环?

database - 无法更改复合类型,因为列正在使用它