将sqlite-net与WCF一起使用时的循环引用

时间:2018-09-06 09:13:58

标签: c# sqlite wcf sqlite-net

sqlite-net extensionsWCF一起使用时,我面临循环引用。下面的代码可以正常工作,但是我没有从RabbitQueueStatRabbitQueue的引用。如果我取消对RabbitQueueStat中的行的注释,则会发生错误。尽管db.GetAllWithChildren<RabbitQueue>().ToList();将为我提供有效的值,但是我在return queues上遇到了错误。

enter image description here

如果我在RabbitQueueRabbitQueueStat上设置断点,我可以看到我正在陷入无限循环。有什么我可以做的吗?

public class RabbitQueue
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Name { get; set; }

    [OneToMany]
    public List<RabbitQueueStat> RabbitQueueStats { get; set; }
}

public class RabbitQueueStat
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public int Ack { get; set; }

    [ForeignKey(typeof(RabbitQueue))]
    public int RabbitQuqueId { get; set; }

    //[ManyToOne]
    //public RabbitQueue RabbitQueue { get; set; }
}

public List<RabbitQueue> GetQueues()
{
    var db = getConnection();
    var queues =  db.GetAllWithChildren<RabbitQueue>().ToList();
    return queues;
}

public int InsertQueue(string name, string iban)
{
    var queue = new RabbitQueue()
    {
        Name = name,
        Iban = iban,
        RabbitQueueAverageStats = new List<RabbitQueueAverageStat>(),
        RabbitQueueStats = new List<RabbitQueueStat>()
    };

    var db = getConnection();
    var inserted = db.Find<RabbitQueue>(x => x.Name == name && x.Iban == iban);
    if (inserted != null)
        return inserted.Id;
    else
        return db.Insert(queue);
}

public void InsertQueueStat(int queueId, int ack)
{
    var queueStat = new RabbitQueueStat()
    {
        Ack = ack,
    };

    var db = getConnection();
    var queue = db.Find<RabbitQueue>(x => x.Id == queueId);
    if (queue != null)
    {
        db.Insert(queueStat);

        if (queue.RabbitQueueStats == null)
            queue.RabbitQueueStats = new List<RabbitQueueStat>();

        queue.RabbitQueueStats.Add(queueStat);
        db.UpdateWithChildren(queue);
    }
}

为清楚起见:

  • 添加RabbitQueue而没有任何RabbitQueueStat的所有内容之后 工作正常
  • 评论public RabbitQueue RabbitQueue { get; set; }后一切正常
  • 问题 当我至少分配了1个RabbitQueueStat时出现 RabbitQueue的{​​{1}}中没有注释的行。

0 个答案:

没有答案