我正在尝试运行update-database
来迁移对数据库所做的某些更改。
一切顺利,直到出现以下错误:
没有为实体类型'ReportType'找到合适的构造函数。的以下构造函数具有无法绑定到的参数实体类型的属性:无法在其中绑定“ id”,“ name”'ReportType(字符串ID,字符串名称)'。
这里是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)
{
}
}
您可以在该代码的注释部分中看到,拥有无参数构造函数通常可以解决EF Core的此问题,但是SmartEnum没有无参数构造函数的基础。
[在Arpil 27th上对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));
在ApplicationDbContext.cs的OnModelCreating中:
modelBuilder.Entity<Report>()
.Property(p => p.ReportType)
.HasConversion(
p => p.Value,
p =>ReportType.FromValue(p));