将sqlite-net extensions
与WCF
一起使用时,我面临循环引用。下面的代码可以正常工作,但是我没有从RabbitQueueStat
到RabbitQueue
的引用。如果我取消对RabbitQueueStat
中的行的注释,则会发生错误。尽管db.GetAllWithChildren<RabbitQueue>().ToList();
将为我提供有效的值,但是我在return queues
上遇到了错误。
如果我在RabbitQueue
和RabbitQueueStat
上设置断点,我可以看到我正在陷入无限循环。有什么我可以做的吗?
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; }
后一切正常RabbitQueueStat
时出现
RabbitQueue
的{{1}}中没有注释的行。