[英]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: 42P07SqlState: 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 exists42P07:关系“所有者”已经存在
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.