使用的是 Entity Framework 5.0 ,运行时报下面的错误,请问如何解决?
System.InvalidOperationException: No suitable constructor was found for entity type 'string'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'value' in 'string(char[] value)'; cannot bind 'value', 'startIndex', 'length' in 'string(char[] value, int startIndex, int length)'; cannot bind 'value' in 'string(Char* value)'; cannot bind 'value', 'startIndex', 'length' in 'string(Char* value, int startIndex, int length)'; cannot bind 'value' in 'string(SByte* value)'; cannot bind 'value', 'startIndex', 'length' in 'string(SByte* value, int startIndex, int length)'; cannot bind 'value', 'startIndex', 'length', 'enc' in 'string(SByte* value, int startIndex, int length, Encoding enc)'; cannot bind 'c', 'count' in 'string(char c, int count)'; cannot bind 'value' in 'string(ReadOnlySpan<char> value)'.
at Microsoft.EntityFrameworkCore.Metadata.Conventions.ConstructorBindingConvention.ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext`1 context)
试试看,如果类型是这么定义的:
class ...
{
public string val;
}
把 public string val改为:
public string val{get;set;}
的确是 string 类型引起的,实际情况是配置实体关系时手误,错误地配置到了string类型的字段上
modelBuilder.Entity<Group>()
.HasOne(g => g.Creator)
应该是
modelBuilder.Entity<Group>()
.HasOne(g => g.GroupCreator)
@dudu: 哦哦,解决了就好!