Cannot pass an object of type "NHibernate.Collection.Generic.PersistentGenericBag" to print "System.Collections.Generic.List"

I am trying to load a domain class by deserializing an xml file. Therefore, I used System.Collections.Generic.List in the domain class. But when I try to save the object using the Session object, it 1[MyFirstMapTest.Class5]' to type 'System.Collections.Generic.List error "Cannot pass an object of type" NHibernate.Collection.Generic.PersistentGenericBag 1[MyFirstMapTest.Class5]' to type 'System.Collections.Generic.List 1 [MyFirstMapTest.Class5 ] ". This problem was published in a previous discussion, and the answer was to use IList instead of List ( Cannot use an object of type NHibernate.Collection.Generic.PersistentGenericBag to List )

But, if I use IList, then I cannot deserialize xml in the Domain class.

 XmlTextReader xtr = new XmlTextReader(@"C:\Temp\SampleInput.xml"); XmlSerializer serializer = new XmlSerializer(objClass5.GetType()); objClass5 = (MyFirstMapTest.Class5)serializer.Deserialize(xtr); session.Save(objClass5); 

Throws an error below "Cannot serialize an xxxxx element of type System.Collections.Generic.IList`1 [[xxxxxxxxxx, Examples, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null]] because it is an interface."

I tried using a PersistentGenericBag instead of a List, but the PersistentGenericBag is not serializable. Therefore, deserialization does not work.

How can I solve this problem? Thanks for looking at this issue.

+4
source share
2 answers

In this class, you can create two properties:

 public class Sample { private IList<Sample> _list; [XmlIgnoreAttribute] public virtual IList<Sample> List { get { return _list; } set { _list = value; } } public virtual List<Sample> List { get { return (List<Sample>)_list; } set { _list = value; } } } 

And you will only draw your IList Property.

-1
source

You can try to use the fallback field to bind NHibernte and the property for Serialization, where the property will be of type List and the field of backing will be IList.

Edit
smooth mapping might look like this:

 public class HierarchyLevelMap : IAutoMappingOverride<HierarchyLevel> { public void Override(AutoMapping<HierarchyLevel> mapping) { mapping.HasMany(x => x.StructuralUnits) .Access.ReadOnlyPropertyThroughCamelCaseField(); } } 

an object:

 public class HierarchyLevel : IEntity { private readonly IList<StructuralUnit> structuralUnits = new List<StructuralUnit>(); public virtual List<StructuralUnit> StructuralUnits { get { return structuralUnits; } set { structuralUnits = value; } } } 
+1
source

Source: https://habr.com/ru/post/1481693/


All Articles