2013-08-05 27 views
0

我有一個繼承List的類,這個類有一些額外的屬性。當我將父文檔存儲在RavenDB中時,列表項將被存儲,但其他屬性不會。繼承的RavenDB問題存儲對象列表<T>

下失敗的測試或許能解釋我的問題更好:

[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> 
{ 
} 

你可以從測試看,字符串屬性「名稱」的佔有類沒有得到保存。

有什麼我需要做的,讓這個屬性堅持?

非常感謝!

回答

0

JSON無法表示既是列表又有屬性的對象。 這就是爲什麼你不能這樣做。您可以擁有一個包含list屬性的對象,這是一種更自然的方式。