首页 新闻 会员 周边 捐助

EF Core 报错"No suitable constructor was found for entity type 'string'"

0
悬赏园豆:30 [已解决问题] 解决于 2020-12-28 23:34

使用的是 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)

dudu的主页 dudu | 高人七级 | 园豆:30757
提问于:2020-12-28 22:05
< >
分享
最佳答案
0

试试看,如果类型是这么定义的:
class ...
{
public string val;
}

把 public string val改为:
public string val{get;set;}

收获园豆:30
PER10 | 初学一级 |园豆:8 | 2020-12-28 23:11

的确是 string 类型引起的,实际情况是配置实体关系时手误,错误地配置到了string类型的字段上

 modelBuilder.Entity<Group>()
    .HasOne(g => g.Creator)

应该是

 modelBuilder.Entity<Group>()
    .HasOne(g => g.GroupCreator)
dudu | 园豆:30757 (高人七级) | 2020-12-28 23:34

@dudu: 哦哦,解决了就好!

PER10 | 园豆:8 (初学一级) | 2020-12-28 23:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册