-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to store classes inherited from a collection. #1211
Comments
Please consider this pull request: |
Hi @gunpal5 , I've been trying to reproduce your case with the classes you provided, without success so far as it correctly (de)serializes the classes. Could you maybe provide some more code so we can possibly reproduce this? Below is the code I wrote in an attempt to reproduce it. I also provided a custom bsonMapper registration in commented code in the case it might help you out (although I'm not sure if it's being used or not when it's not commented out). public class SomeItem
{
public string SomeProperty { get; set; }
}
public class SomeItemCollection : ObservableCollection<SomeItem>
{
//Codes..... //Codes...
}
public class MyProjectClass
{
[BsonId]
public string ProjectName { get; set; }
public SomeItemCollection Items { get; set; }
}
static void Main(string[] args)
{
var projectClass = new MyProjectClass
{
ProjectName = "Test",
Items = new SomeItemCollection
{
new SomeItem {SomeProperty = "Test 1"},
new SomeItem {SomeProperty = "Test 2"},
new SomeItem {SomeProperty = "Test 3"}
}
};
var liteDb = new LiteDatabase(new MemoryStream()); // In-memory database for testing purposes.
/*liteDb.Mapper.RegisterType<SomeItemCollection>(obsCol => new BsonArray(obsCol.Select(obj => liteDb.Mapper.ToDocument(obj))), bsonValue =>
{
var coll = new SomeItemCollection();
var bsonArray = bsonValue.AsArray;
foreach (var bsonValueOfArray in bsonArray)
{
coll.Add(liteDb.Mapper.ToObject<SomeItem>(bsonValueOfArray.AsDocument));
}
return coll;
});*/
var myProjectClassCollection = liteDb.GetCollection<MyProjectClass>();
myProjectClassCollection.Insert(projectClass);
var deserializedProjectClass = myProjectClassCollection.FindOne(Query.All());
} |
@gunpal5 I believe my answer resolves your question so I'm closing the issue. Feel free to re-open the issue if you feel like your issue hasn't been resolved yet. |
Hello,
I have many desktop applications developed over last 4-5 years. I recently needed to change data storage used in the software. I decided to use LiteDb. The problem I am facing is that I have most of the classes inherited from ObservableCollection. e.g.
public class SomeItem { public string SomeProperty{get;set;} } }
public class SomeItemCollection:ObservableCollection<SomeItem> { //Codes..... //Codes... }
public class MyProjectClass { public string ProjectName{get;set;} public SomeItemCollection Items{get;set;} }
Now whenever I try to store the project classes which have many properties like SomeItemCollection in example, I get this exception:
Is there anyway to register a custom bson mapper so that I can map the classes in my project into bson. The classes I am trying to store into the database can be serialized without any issue using Newtonsoft.Json.JsonConvert.Serialize method, but some how litedb is unable to serialize them.
Thanks
Gunpal Jain
The text was updated successfully, but these errors were encountered: