简体   繁体   English

EF Core:更新数据库错误:关系“所有者”已经存在

[英]EF Core: update database error: relation "Owner" already exists

I try to add migration:我尝试添加迁移:

dotnet ef migrations add InitialCreate

and then update the database:然后更新数据库:

dotnet ef database update

But, I get an error:但是,我收到一个错误:

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

Failed executing DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']执行 DbCommand 失败 (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE "Owner" (创建表“所有者”(
"Id" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY, “Id”整数 NOT NULL GENERATED BY DEFAULT AS IDENTITY,
reputation integer NOT NULL, user_id integer NOT NULL,声誉整数非空,user_id 整数非空,
user_type text NULL, user_type 文本 NULL,
profile_image text NULL, profile_image 文本 NULL,
display_name text NULL, display_name 文本 NULL,
link text NULL,链接文本 NULL,
accept_rate integer NULL, accept_rate 整数 NULL,
CONSTRAINT "PK_Owner" PRIMARY KEY ("Id")约束“PK_Owner”主键(“Id”)
); );

Npgsql.PostgresException (0x80004005): 42P07: relation "Owner" already exists Npgsql.PostgresException (0x80004005): 42P07: 关系“所有者”已经存在

at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<g__ReadMessageLong|0>d.MoveNext()在 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()在 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)在 Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
at Npgsql.NpgsqlDataReader.NextResult()在 Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)在 Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior 行为,布尔异步,CancellationToken 取消令牌)
at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)在 Npgsql.NpgsqlCommand.ExecuteNonQuery(布尔异步,CancellationToken 取消令牌)
at Npgsql.NpgsqlCommand.ExecuteNonQuery()在 Npgsql.NpgsqlCommand.ExecuteNonQuery()

Exception data:异常数据:
Severity: ERROR严重性:错误
SqlState: 42P07 SqlState: 42P07
MessageText: relation "Owner" already exists消息文本:关系“所有者”已存在
File: heap.c文件:heap.c
Line: 1155线路:1155
Routine: heap_create_with_catalog例程:heap_create_with_catalog
42P07: relation "Owner" already exists 42P07:关系“所有者”已经存在

Here on my github i store model (and other source code of service). 在我的 github 上,我存储模型(和其他服务源代码)。 I remove 'EnsureCreate' code from ApplicationContext file.我从 ApplicationContext 文件中删除了“EnsureCreate”代码。

How to solve this error?如何解决这个错误? Thank you!谢谢!

PS I remove file appsettings.json where store connetion string: PS我删除了存储连接字符串的文件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"
  ]
  }

In your line 38 I see a problem where you use filed but you need to use the property.在您的第 38 行中,我看到您使用已归档但您需要使用该属性的问题。

public Owner Owner { get; set; }

But you use但是你用

public Owner owner { get; set; }

and that's a problem.这是一个问题。

Another solution: as I understand to see your problem that owner table relation already exists.另一个解决方案:据我了解,您的问题是所有者表关系已经存在。 So delete unnecessary relation or use Fluent Api.所以删除不必要的关系或使用 Fluent Api。

使用 DB Management Studio 或命令行删除Owner表,然后再次运行迁移。

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

相关问题 EF Core Postgres Update-Database 正在尝试创建已经存在的数据库 - EF Core Postgres Update-Database is trying to Create Database which already exists EF-Core:表“名称”已存在 - 尝试更新数据库时 - EF-Core: Table "name" already exists - when trying to update database EF6 中的关系错误外键已存在 - Foreign Key already exists For Relation Error in EF6 为什么 EF Core 会抛出“数据库已存在”? - Why does EF Core throws "Database already exists"? EF Core Add-Migration 错误属性已存在 - EF Core Add-Migration error property already exists Ef core code first Error: Key ("Id")=(33) already exists - Ef core code first Error : Key ("Id")=(33) already exists 无法在 EF Core 中使用迁移:“42P07:关系”AspNetRoles“已经存在” - Can't use migrations in EF Core: “42P07: relation ”AspNetRoles“ already exists” 将列表与数据库记录进行比较,如果存在 - 更新,如果新 - 创建(EF Core) - Compare List to Database records, if exists - update, if new - create (EF Core) EF Core 3.1 数据库首次删除关系错误 - EF Core 3.1 database first removed relation error 实体A包含实体B,我如何在不插入B的情况下更新A(因为它已经存在)EF核心 - Entity A contains Entity B, how do I Update A without inserting B (cause it already exists) EF core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM