我尝试添加迁移:
dotnet ef migrations add InitialCreate
然后更新数据库:
dotnet ef database update
但是,我得到一个错误:
Build started...
Build succeeded.
[21:59:12 INF] OnConfigure finish
Applying migration '20201029182606_InitialCreate'.
无法执行DbCommand(7毫秒)[Parameters = [],CommandType ='Text',CommandTimeout = '30']
创建表“所有者”(
默认值为ID的“ Id”整数NOT NULL,
信誉整数NOT NULL, user_id整数NOT NULL,
user_type文本为NULL,
profile_image文本为NULL,
display_name文本为NULL,
链接文字为NULL,
accept_rate整数NULL,
CONSTRAINT“ PK_Owner”主键(“ Id”)
);Npgsql.PostgresException(0x80004005):42P07:关系“所有者”已存在
Npgsql.NpgsqlConnector上的。<> c__DisplayClass160_0。
d.MoveNext()
---先前位置的堆栈跟踪结束---
在Npgsql.NpgsqlConnector。<> c__DisplayClass160_0。d.MoveNext()
---先前位置的堆栈跟踪结束---
在Npgsql.NpgsqlDataReader.NextResult(布尔异步,布尔isConsuming)
在Npgsql.NpgsqlDataReader.NextResult()
在Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior,Boolean async,CancellationToken cancelToken)
在Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean异步,CancellationToken cancellingToken)
在Npgsql.NpgsqlCommand.ExecuteNonQuery()异常数据:
严重性:错误
SqlState:42P07
MessageText:关系“所有者”已存在
文件:heap.c
线:1155
例程:heap_create_with_catalog
42P07:关系“所有者”已存在
Here on my github我存储模型(和其他服务源代码)。 我从ApplicationContext文件中删除了“ EnsureCreate”代码。
如何解决此错误? 谢谢!
P.S。我删除了存储连接字符串的文件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"
]
}
答案 0 :(得分:1)
在您的第38行中,我看到一个在使用字段但需要使用该属性的问题。
public Owner Owner { get; set; }
但是您使用
public Owner owner { get; set; }
那是个问题。
另一个解决方案:据我了解,您看到所有者表关系已经存在的问题。因此,删除不必要的关系或使用Fluent Api。
答案 1 :(得分:0)
使用DB Management Studio或命令行删除Owner
表,然后再次运行迁移。