我有一个从 List 继承的类,这个类有一些额外的属性。当我将父文档存储在 RavenDB 中时,会存储列表项,但不会存储其他属性。
下面的失败测试可能更好地解释了我的问题:
[TestFixture]
public class RDBIssueTests
{
private DocumentStore _documentStore;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
_documentStore = new EmbeddableDocumentStore
{
RunInMemory = true,
UseEmbeddedHttpServer = true,
DataDirectory = "Data",
};
_documentStore.Initialize();
}
[Test]
public void StoreSimpleDataTest()
{
string id = "people/1";
string laptopName = "MacPro";
string personName = "Joe Bloggs";
string attrOne = "Screen";
string attrTwo = "Keyboard";
var person = new Person()
{
Id = id,
Name = personName,
Laptop = new Possession<string>()
};
person.Laptop.Name = laptopName;
person.Laptop.Add(attrOne);
person.Laptop.Add(attrTwo);
using (var session = _documentStore.OpenSession())
{
session.Store(person);
session.SaveChanges();
}
using (var session = _documentStore.OpenSession())
{
var loadedPerson = session.Load<Person>(id);
Assert.AreEqual(personName, loadedPerson.Name);
Assert.AreEqual(2, loadedPerson.Laptop.Count); // 2 items in the list
Assert.IsTrue(loadedPerson.Laptop.Contains(attrOne));
Assert.IsTrue(loadedPerson.Laptop.Contains(attrTwo));
Assert.AreEqual(laptopName, loadedPerson.Laptop.Name); // fails here - Person.Laptop.Name is not persisted in RBD
}
}
}
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
public Possession<string> Laptop { get; set; }
}
public class Possession<TValueType> : PossessionAttribute<TValueType>
{
public string Name { get; set; } // RDB doesn't persist this value
}
public class PossessionAttribute<TValueType> : List<TValueType>
{
}
正如您从测试中看到的那样,Possession 类上的字符串属性“名称”没有得到保存。
我需要做些什么来让这个属性持续存在吗?
非常感谢!