【发布时间】:2021-01-05 16:48:59
【问题描述】:
我已手动将列 IdCategory 添加到我的表 Books 中,现在每次尝试运行 Update-Database 时,都会出现此错误:
失败:Microsoft.EntityFrameworkCore.Database.Command[20102]
执行 DbCommand 失败 (43ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [书籍] ADD [IdCategory] nvarchar(max) NULL;执行 DbCommand 失败 (43ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
每个表中的列名必须是唯一的。表“Books”中的列名“IdCategory”被指定了多次。
我在许多论坛上都发现了此类问题,但解决方案在我的情况下并没有解决。
我该如何解决这个问题?
这是我的模型:
public class Book
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
public int IdCategory { get; set; }
}
public class BookCategory
{
[Key]
public int IdCategory { get; set; }
[Required]
public string Description { get; set; }
}
【问题讨论】:
-
那么,您的情况是什么?您显然正在执行
ALTER TABLE [Books] ADD [IdCategory] nvarchar(max) NULL,但该列已经存在。我们无法为您提供任何帮助。 -
你能显示实体类
Book和Category吗? -
Book的category属性不应该是“BookCatelgory”类型的导航属性吗?
-
如果您还使用迁移,则无法进行“手动”数据库更新
-
我问是否有办法“重置”并使用实际模型简单地更新数据库。
标签: c# sql-server entity-framework .net-core