Protobuf C#生成的类用法,实体框架重复字段不可设置
我在将一些Protobuf消息生成的C#类与实体框架核心集成时遇到了一些问题。在大多数情况下,我能够使用protobuf生成的类将信息正确地存储到sqlite和mysql数据库中。protobuf消息使用重复变量时会出现问题。我可以通过序列化到字符串列表将重复字段存储到数据库中,但是当试图从数据库中读取时,我得到了这个错误Protobuf C#生成的类用法,实体框架重复字段不可设置,c#,entity-framework-core,protocol-buffers,C#,Entity Framework Core,Protocol Buffers,我在将一些Protobuf消息生成的C#类与实体框架核心集成时遇到了一些问题。在大多数情况下,我能够使用protobuf生成的类将信息正确地存储到sqlite和mysql数据库中。protobuf消息使用重复变量时会出现问题。我可以通过序列化到字符串列表将重复字段存储到数据库中,但是当试图从数据库中读取时,我得到了这个错误 Unhandled Exception: System.InvalidOperationException: No backing field could be found
Unhandled Exception: System.InvalidOperationException: No backing field could be found for property '<propertyname>' of entity type '<entityname>' and the property does not have a setter.
其中,数据作为JSON样式的字符串写入数据库,然后使用自定义转换将其转换回重复字段
提前谢谢你的帮助
编辑:目前正在使用Google.Protobuf(3.6.1)库这里的问题是消息:
syntax=“proto3”;
留言富{
重复串条=1;
}
protoc
生成:
private readonly pbc::RepeatedField bar_=new pbc::RepeatedField();
[全局::System.Diagnostics.DebuggerNonUserCodeAttribute]
公共pbc::RepeatedField条{
获取{返回条}
}
但是这个url\uuu
与任何预期模式都不匹配;具体而言,在这种情况下,EF Core将接受\u Bar
、\u Bar
、m\u Bar
或m\u Bar
——但不接受Bar
若你们愿意改变图书馆,你们可以尝试使用protobuf网络;(protobuf net相当于protoc
)生成:
[global::ProtoBuf.ProtoMember(1,Name=@“bar”)]
public global::System.Collections.Generic.List条{get;}
=新的全局::System.Collections.Generic.List();
现在;目前还不完全清楚EF是否会理解这种模式,但这最终是Roslyn自动生成的模式(字段将是k_ubackingfield
),因此至少值得一试。大多数ORMs和序列化程序都理解Roslyn模式
否则,您需要查看上述文档的后面部分,其中描述了显式配置支持字段名称的方法。您使用的是哪个protobuf库?谷歌?Protobuf net?很抱歉使用Google.Protobuf库,目前是3.6.1
entity.Property(e => e.StationTypes)
.HasConversion(
types => types.ToString(),
column => ConvertToRepeatedField(column)
);