如何在LiteDb中连接两个表,如SQL示例:我有两个表用户和ActivityLog
这是模型
public class ActivityLog
{
[BsonId]
public int Id { get; set; }
public string UserId { get; set; }
public string Action { get; set; }
public DateTime ActionDateTime { get; set; }
}
public class User
{
[BsonId]
public int Id { get; set; }
public string UserId { get; set; }
public string UserName { get; set; }
public DateTime LoginDate { get; set; }
}我需要加入Activity.UserID = User.UserId。有像sql这样的连接方式吗?
发布于 2017-07-07 14:58:33
来自官方文档
LiteDB是一个文档数据库,因此集合之间没有连接。如果需要引用另一个文档中的文档,可以使用DbRef。可以在初始化数据库或运行查询时或在查询完成后加载此文档引用。
在你的例子中,你可以做类似的事情
public class ActivityLog
{
[BsonId]
public int Id { get; set; }
public DbRef<User> User { get; set; }
public string Action { get; set; }
public DateTime ActionDateTime { get; set; }
}
public class User
{
[BsonId]
public int Id { get; set; }
public string UserId { get; set; }
public string UserName { get; set; }
public DateTime LoginDate { get; set; }
}
//usage
// Getting user and activityLog collections
var usersCollection = db.GetCollection<User>("Users");
var activityLogsCollection = db.GetCollection<ActivityLog>("ActivityLogs");
// Creating a new User instance
var user = new User { UserId = 5, ...};
usersCollection.Insert(user);
// Create a instance of ActivityLog and reference to User John
var activityLog = new ActivityLog
{
OrderNumber = 1,
OrderDate = DateTime.Now,
//Add it by DbRef
User = new DbRef<User>(usersCollection, user.UserId)
};
activityLogsCollection.Insert(activityLog)有关详细信息,请参阅文档。
https://stackoverflow.com/questions/44675677
复制相似问题