gpt4 book ai didi

c# - 如何同步 nosql db (ravendb) 中的更改

转载 作者:行者123 更新时间:2023-11-30 14:01:45 25 4
gpt4 key购买 nike

我已经开始通过 RavenDB 示例学习 NoSQL。我从一个最简单的模型开始,假设我们有用户创建的主题:

public class Topic
{
public string Id { get; protected set; }
public string Title { get; set; }
public string Text { get; set; }

public DenormalizedUser User { get; set; }
}

public class DenormalizedUser
{
public string Id { get; set; }
public string Name { get; set; }
}

public class User
{
public string Id { get; protected set; }
public string Name { get; set; }
public DateTime Birthdate { get; set; }

//some other fields
}

我们不需要整个 User 来显示 Topic,所以我将它非规范化为 DenormalizedUser,其中包含一个 Id 和一个 Name

所以,这里是问题:

1) 这种方法适用于 NoSQL 吗?

2) User 更改Name 时如何处理?我是否需要手动更新非规范化类中的所有 Name 字段?

最佳答案

Shaddix,您可以使用 Raven DB Include 函数使用主题中的 UserId 加载用户。

var topic = _session.Load<Topic>(topicId)
.Customize(x => x.Include<Topic>(y => y.UserId));

var user = _session.Load<User>(topic.UserId);

主题的加载将“预加载”用户,并且两个加载只会产生一个 GET 请求。 (由于我的声誉,我无法直接回复您对 Ayende 的回复)。

您还可以使用不带 Customize() 的替代(并且可能更清晰).Include() 函数。

http://docs.ravendb.net/consumer/querying/handling-document-relationships.html

关于c# - 如何同步 nosql db (ravendb) 中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7753296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com