Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在RavenDB的某些字段中存储具有空值的实体_C#_Nosql_Ravendb - Fatal编程技术网

C# 在RavenDB的某些字段中存储具有空值的实体

C# 在RavenDB的某些字段中存储具有空值的实体,c#,nosql,ravendb,C#,Nosql,Ravendb,我正在使用RavenDB(NoSQL数据库),我有这样一个类: public class VirtualDirectory { public string Id { get; set; } public string HostAlias { get; set; } public string VirtualPath { get; set; } public string IdentificationMethod { g

我正在使用RavenDB(NoSQL数据库),我有这样一个类:

public class VirtualDirectory
    {   
        public string Id { get; set; }
        public string HostAlias { get; set; }
        public string VirtualPath { get; set; }
        public string IdentificationMethod { get; set; }
        public int IsCancelled { get; set; }
    }
我正在通过以下方式启动此类的新实例:

VirtualDirectory vd = new VirtualDirectory() { HostAlias = "bla", IsCancelled = 0 };
并通过以下方式保存文档:

using (var session = DbConnection.Instance.DocumentStore.OpenSession(DbName))
            {
                session.Store(vd);
                session.SaveChanges();
            }
根据我对NoSQL DB文档的了解,这些文档没有绑定到模式,因此它们的结构是灵活的

我的问题是: 如何将数据保存到RavenDB数据库中,而不在未设置的字段中获取空值?在最后一段代码之后,my db看起来像:

{
 HostAlias : "bla",
 VirtualPath : null,
 IdentificationMethod : null,
 IsCancelled : 0
}
当我得到一个C#对象(VirtualDirectory类型)时,它缺少一些字段,如:

{
 HostAlias : "bla",
 IsCancelled : 0
}

它是否具有空值?

我相信raven会将您的对象(及其所有属性)映射到Json对象并保存它。因此 您的对象属性将被持久化

我将创建另一个对象,它正好具有我需要的属性,并将其保存到数据库中

如果您将VirtualDirectory类保存到raven,raven将实例化您的对象,您将使用空值将其返回,然后填充它具有值的属性

这有用吗

编辑:

在你的评论之后,我做了一个小实验,发现使用“动态”可以很容易地实现你所需要的。Raven会很好地存储它,但是它们不会自动聚合到集合中


听起来怎么样就个人而言,我不知道为什么要避免数据库中的空属性

我喜欢使用RavenDb,拥有空属性对我来说是非常棒和有用的。我相信,如果属性存在于DB中,但不存在于类中(因为类“schema”已更改),那么它将忽略该DB属性/值

也就是说,当RavenDb将某些内容保存到DB中时,您可以告诉它该做什么。在这里,您需要创建一个自定义的
ContractResolver

在这种情况下,需要检查当前值是否为null。如果是,则忽略此属性

所以,这是150%未经测试,只是写在这里后,做了一个快速的谷歌搜索。作为参考

public class IgnoreNullValuesContractResolver : DefaultRavenContractResolver
{
    public IgnoreNullValuesContractResolver(bool shareCache)
        : base(shareCache)
    {
    }

    protected override JsonProperty CreateProperty(MemberInfo member,
                                                   MemberSerialization memberSerialization)
    {
        // Grab the property.
        var property = base.CreateProperty(member, memberSerialization);

        // Only write this property if the value is NOT null.
        // NOTE: I have no idea if this is the correct way to retrieve
        //       the reflected property and it's value. 
        property.Writable = ((PropertyInfo) member).GetValue(member, null) != null;

        return property;
    }
}

GL:)

使用此选项在ravendb中保存:

store.Conventions.CustomizeJsonSerializer = serializer => serializer.NullValueHandling=NullValueHandling.Ignore;
要获取和返回数据,请执行以下操作:

var config = GlobalConfiguration.Configuration;

var settings = config.Formatters.JsonFormatter.SerializerSettings;

settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;

为可能是..ofc not的每个属性组合创建对象是没有意义的-我以为您只想要指定为对象子集的两个属性