简体   繁体   English

如何使用 protobuf.net 序列化循环引用的 ef poco class

[英]How to serialize circular referenced ef poco class using protobuf-net

I'm using marc gravell's protobuf.net and ef core in my project.我在我的项目中使用了 marc gravell 的 protobuf.net 和 ef core。

long story short, I'm using Inverseproperty attribute on my POCO class which causes a circular reference when I fetch results from database which causes me trauble when I try to serialize data using protobuf.net.长话短说,我在我的 POCO class 上使用了 Inverseproperty 属性,当我从数据库中获取结果时会导致循环引用,这会导致我在尝试使用 protobuf.net 序列化数据时遇到麻烦。

I'm currenyl serializing data with Jsonconvert by setting ReferenceLoopHandling = ReferenceLoopHandling.Ignore and returning a json string to the client to keep the app in a working state but do not want to use this method as it doesnot make any sense.我正在使用 Jsonconvert 序列化数据,方法是设置ReferenceLoopHandling = ReferenceLoopHandling.Ignore并将 json 字符串返回给客户端以保持应用程序正常运行 state 但不想使用此方法,因为它没有任何意义。

I would like to know if it is possible to either prevent EF core generating circular reference when using Inverseproperty attribute or if protobuf.net has an ignore referenceloop handling feature when serializing data..我想知道是否有可能在使用 Inverseproperty 属性时阻止 EF 核心生成循环引用,或者 protobuf.net 在序列化数据时是否具有忽略引用循环处理功能。

a simplified version of my poco class is like this:我的 poco class 的简化版本是这样的:

[ProtoContract]
[Table("CATEGORIES_M")]
public class CATEGORIES_M
{
    public CATEGORIES_M()
    {
        CATEGORIES_M_COLLECTION = new HashSet<CATEGORIES_M>();
        //Product = new HashSet<Product>();
        CM_DATE = DateTime.Now;
    }

    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [ProtoMember(1)]
    public int CM_ROWID { get; set; }

    [ProtoMember(2)]
    public string CM_NAME { get; set; }

    [ProtoMember(3)]
    public int? CM_PARENT_REFNO { get; set; }

    [ProtoMember(4)]
    [ForeignKey(nameof(CM_PARENT_REFNO))]
    [InverseProperty(nameof(CATEGORIES_M_COLLECTION))]
    public  CATEGORIES_M CATEGORIES_M_PARENT { get; set; }

    [ProtoMember(5)]
    [InverseProperty(nameof(CATEGORIES_M_PARENT))]
    public  ICollection<CATEGORIES_M> CATEGORIES_M_COLLECTION { get; set; }  
}

any help is appreciated任何帮助表示赞赏

Protobuf.net does not have good support for this scenario. Protobuf.net 对这种情况没有很好的支持。 V2 has some limited reference tracking capabilities, but these are deprecated in V3 because it caused more problems than it solved. V2 具有一些有限的引用跟踪功能,但这些在 V3 中已被弃用,因为它造成的问题多于解决的问题。 My suggestions, as the library author:作为图书馆作者,我的建议是:

  1. serialize a simple tree model, and build your real model afterwards from it, or序列化一棵简单的树 model,然后从中构建真正的 model,或者
  2. use a different tool使用不同的工具

Backreferences (parent level) can be tagged with [ProtoIgnore] to avoid circular references.可以使用[ProtoIgnore]标记反向引用(父级别)以避免循环引用。 That might change the behavior as a client might expect values there.这可能会改变行为,因为客户可能会期望那里的价值。 Though, usually the client has the parent objects already and you might just need a key here.虽然,通常客户端已经有了父对象,你可能只需要一个键。 If that's the case, add an additional serializable property for the key value and mark as [ProtoMember(nn)] , then.如果是这种情况,请为键值添加一个额外的可序列化属性并标记为[ProtoMember(nn)] ,然后。

Hint : Write test cases and use the Serialize class's static methods to check the behavior and get useful exceptions before trying to debug your server code.提示:编写测试用例并使用Serialize类的 static 方法来检查行为并在尝试调试服务器代码之前获取有用的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM