c# - EF核心: update database error: relation "Owner" already exists

标签 c# entity-framework-core

我尝试添加迁移:

dotnet ef migrations add InitialCreate

然后更新数据库:

dotnet ef database update

但是,我收到一个错误:

Build started...
Build succeeded.
[21:59:12 INF] OnConfigure finish
Applying migration '20201029182606_InitialCreate'.

Failed executing DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE "Owner" (
"Id" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY,
reputation integer NOT NULL, user_id integer NOT NULL,
user_type text NULL,
profile_image text NULL,
display_name text NULL,
link text NULL,
accept_rate integer NULL,
CONSTRAINT "PK_Owner" PRIMARY KEY ("Id")
);

Npgsql.PostgresException (0x80004005): 42P07: relation "Owner" already exists

at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location ---
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
at Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteNonQuery()

Exception data:
Severity: ERROR
SqlState: 42P07
MessageText: relation "Owner" already exists
File: heap.c
Line: 1155
Routine: heap_create_with_catalog
42P07: relation "Owner" already exists

Here on my github我存储模型(和其他服务源代码)。 我从 ApplicationContext 文件中删除了“EnsureCreate”代码。

如何解决这个错误? 谢谢!

附注我删除文件 appsettings.json 其中存储连接字符串:

 {
"Logging": {
 "LogLevel": {
   "Default": "Information",
   "Microsoft": "Warning",
   "Microsoft.Hosting.Lifetime": "Information"
 }
},
 "AllowedHosts": "*",
 "ConnectionStrings": {
  "Questions": "Host=localhost;Port=5432;Database=questiondb;Username=postgres;Password=password"
 },
  "Tags": [
   "ef core",
   "c#",
   "asp.net core",
   "asp.net core webapi"
  ]
  }

最佳答案

在您的第 38 行中,我看到一个问题,您使用了归档,但您需要使用该属性。

public Owner Owner { get; set; }

但是你使用

public Owner owner { get; set; }

这是一个问题。

另一个解决方案:据我了解,您的问题是所有者表关系已经存在。因此删除不必要的关系或使用Fluent Api。

关于c# - EF核心: update database error: relation "Owner" already exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64597649/

相关文章:

mysql - 在 EntityFrameworkCore 中加载相关数据时出错

c# - EF Core - 如何使用值对象审计跟踪

c# - 64 位内存不足异常

c# - 时钟速度公式

c# - ASP.NET 核心 : Idle timeout between the calls with a delay

c# - 如何在 EF Core 表达式中使用继承的属性?

c# - 将 AspNetCore.TestHost 与 EntityFrameworkCore.InMemory 一起使用丢失数据

c# - 使用StructureMap注入(inject)相应的实现

c# - 在 C# 和 JavaScript 之间传递元素

.NET Core Entity Framework - 迁移路径 --output-dir