我正在测试 LiteDB 数据库,但在更新数据时遇到问题。让我们考虑源代码:
public class Session
{
[BsonId]
public int Id { get; set; }
public Guid SessionGuid { get; set; }
public DateTime Date { get; set; }
public double TotalAmount { get; set; }
[BsonRef("paymentInfos")]
public PaymentInfo PaymentInfos { get; set; }
}
public class PaymentInfo
{
[BsonId]
public int Id { get; set; }
public string Type { get; set; }
public double Amount { get; set; }
}
_database = new LiteDatabase(databasePath);
var sessions = _database.GetCollection<Session>("sessions");
var sessionId = Guid.NewGuid();
var session = new Session
{
SessionGuid = sessionId,
Date = Datetime.Now
};
sessions.Insert(session);
var sessions = _database.GetCollection<Session>("sessions");
var session = sessions.FindOne(x => x.SessionGuid == sessionId);
var paymentInfo = new PaymentInfo
{
Type = "Coin",
Amount = 2.0,
};
var paymentInfos = _database.GetCollection<PaymentInfo>("paymentInfos");
paymentInfos.Insert(paymentInfo);
session.PaymentInfos = paymentInfo;
sessions.Update(session);
session = sessions.FindOne(x => x.SessionGuid == sessionId);
var paymentAmount = session.PaymentInfos.Amount;
我原以为 paymentAmount 将是 2.0,但它只有 0。这就像只有 session.PaymentInfos.Id 很好,但所有其他属性都丢失了。我的会话更新有问题吗?也许我错过了什么?
提前致谢。