简体   繁体   English

无法在 EF Core 中使用迁移:“42P07:关系”AspNetRoles“已经存在”

[英]Can't use migrations in EF Core: “42P07: relation ”AspNetRoles“ already exists”

I have strange issue which I can't find solution for.我有一个奇怪的问题,我找不到解决方案。

The stack is: NET Core 2, EF, PostgreSQL.堆栈是:NET Core 2、EF、PostgreSQL。 I use .NET Core Identity with User : IdentityUser to extend base user model with additional fields.我将 .NET Core Identity 与User : IdentityUser IdentityUser 一起User : IdentityUser来扩展具有附加字段的基本用户模型。

After I create first migration, drop whole database and try to dotnet ef database update I always get an error: 42P07: relation "AspNetRoles" already exists创建第一次迁移后,删除整个数据库并尝试dotnet ef database update我总是收到一个错误: 42P07: relation "AspNetRoles" already exists

Even with this error, the database and tables are created but it makes migrations useless as it does not save applied migrations so I can't update DB with following changes...即使出现此错误,也会创建数据库和表,但它使迁移变得无用,因为它不保存应用的迁移,因此我无法使用以下更改更新数据库...

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") );失败:Microsoft.EntityFrameworkCore.Database.Command[200102] 执行 DbCommand 失败 (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE "AspNetRoles" ("Id" text NOT NULL, " ConcurrencyStamp”文本NULL,“名称”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() Npgsql.PostgresException (0x80004005): 42P07: 关系“AspNetRoles”已经存在于 Npgsql.NpgsqlConnector.d__148.MoveNext() --- 从上一个抛出异常的位置的堆栈跟踪结束 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo .Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at System.Runtime.CompilerServices.ValueTaskAwaiter 1.GetResult()
at Npgsql.NpgsqlConnector.d__147.MoveNext()在 Npgsql.NpgsqlConnector.d__147.MoveNext()

The reason was pretty simple.原因很简单。 I was calling EnsureCreated in Startup.cs which was getting conflict with migrations as working different way.我在Startup.cs中调用了EnsureCreated ,它与迁移发生冲突,因为它以不同的方式工作。 Thankfully EF Core owners made it clear for me on GitHub.谢天谢地,EF Core 所有者在 GitHub 上向我明确说明了这一点。

So to summarize - if you want to use Migrations, you can't use EnsureCreated.总而言之 - 如果您想使用迁移,则不能使用确保创建。

I was doing something really dumb!我在做一些非常愚蠢的事情! I copied the connection string from another micro service and it had the wrong Database in the connection string - that database already had the table that the error was complaining about!我从另一个微服务复制了连接字符串,它在连接字符串中有错误的数据库 - 该数据库已经有错误所抱怨的表! Block Copy strikes again!块复制再次来袭!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM