eva*_*ntw 6 c# entity-framework entity-framework-core asp.net-core entity-framework-migrations
我正在尝试运行update-database以迁移我对数据库所做的一些更改。
一切顺利,直到出现以下错误:
找不到适合实体类型“ReportType”的构造函数。以下构造函数的参数无法绑定到实体类型的属性:无法绑定 'id', 'name' in 'ReportType(string id, string name)'。
这是 ReportType.cs 的代码:
public class ReportType : SmartEnum<ReportType, string>
{
public static readonly ReportType ReportType1 = new ReportType("Blah", "Blah");
public static readonly ReportType ReportType2 = new ReportType("Blah", "Blah");
public static readonly ReportType ReportType3 = new ReportType("Blah", "Blah");
// required for EF, but breaking for SmartEnum
// private ReportType() {}
private ReportType(string id, string name) : base(name, id)
{
}
}
Run Code Online (Sandbox Code Playgroud)
正如您在该代码的注释部分所看到的,拥有无参数构造函数通常可以解决 EF Core 的这个问题,但 SmartEnum 没有无参数构造函数基础。
2018 年 2 月 27 日 Arpil 对 SmartEnum 库进行了提交,该库添加了一个无参数构造函数,因此该问题不存在,但该更改在以后的提交中被删除,我不确定没有它如何继续。
该提交可以在这里找到:https : //github.com/ardalis/SmartEnum/commit/870012d406609a4a8889fdde2139750dc618d6a9
并在此提交中删除:https : //github.com/ardalis/SmartEnum/commit/1c9bf3ede229fcb561330719cd13af67dcf92ad7
任何帮助是极大的赞赏!
编辑:
根据 Ivan 的评论,这是我对此问题的解决方案:
modelBuilder.Entity<Report>()
.Property(p => p.ReportType)
.HasConversion(
p => p.Value,
p =>ReportType.FromValue(p));
Run Code Online (Sandbox Code Playgroud)
在ApplicationDbContext.cs的OnModelCreating中:
modelBuilder.Entity<Report>()
.Property(p => p.ReportType)
.HasConversion(
p => p.Value,
p =>ReportType.FromValue(p));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1692 次 |
| 最近记录: |