Failed to execute command "NHibernate.Collection.Generic.PersistentGenericSet`1 in System.Collections.Generic.IList`1

I have a domain class:

public class Agencia : IEntity { public virtual int Id { get; set; } public virtual string Nome { get; set; } public virtual string Identificacao { get; set; } public virtual IList<Pessoa> Gerentes { get; protected set; } public Agencia() { Gerentes = new List<Pessoa>(); } public virtual void AddGerente(Pessoa gerente) { Gerentes.Add(gerente); } public virtual void AddGerentes(params Pessoa[] gerentes) { Parallel.ForEach(gerentes, (pessoa) => Gerentes.Add(pessoa)); } } public class Pessoa: IEntity { public virtual int Id { get; set; } public virtual string Nome { get; set; } } 

Using this convention (defined as set AsSet )

 public class AgenciaConvention : IAutoMappingOverride<Agencia> { public void Override(AutoMapping<Agencia> mapping) { mapping.HasManyToMany(a => a.Gerentes).Cascade.AllDeleteOrphan().AsSet().Not.Inverse(); } } 

When I run this test:

 [TestMethod] [Description("Uma agência tem vários gerêntes")] public void AgenciaTemVariosGerentes() { // Arrange var fix = new Fixture(); var currentUser = GetLoggedUser(); // Create a List<Pessoa> var gerentes = fix.Build<Pessoa>() .With(p => p.Nome) .With(p => p.CPF) .With(p => p.CreateBy, currentUser) .OmitAutoProperties() .CreateMany<Pessoa>(10).ToList(); // Action new PersistenceSpecification<Agencia>(Session) .CheckProperty(p => p.Nome, fix.Create<string>().Truncate(80)) .CheckProperty(p => p.Identificacao, fix.Create<string>().Truncate(10)) .CheckReference(p => p.Regional, fix.Build<Regional>() .With(p => p.Nome) .OmitAutoProperties() .Create() , new IDEqualityComparer()) .CheckList(p => p.Gerentes, gerentes, new IDEqualityComparer()) .CheckReference(p => p.CreateBy, currentUser, new IDEqualityComparer()) .VerifyTheMappings(); // Assert } 

How to check this list?

The collection must be AsSet , it is necessary that the "Parent and child" fields be PK, FK

Full error:

Test Name: AgenciaTemVariosGerentes Test FullName: {OMMITED} .Integration.Test.AgenciaTest.AgenciaTemVariosGerentes Test Source: {OMMITED} .Integration.Test \ AgenciaTest.cs: line 22 Test Result: Failure Test Duration: 0: 00: 02,4093555

Message: Testing method {OMMITED} .Integration.Test.AgenciaTest.AgenciaTemVariosGerentes threw an exception: NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type inconsistencies); setter of CreditoImobiliarioBB.Model.Regional ---> System.InvalidCastException: Cannot reset an object of type "NHibernate.Collection.Generic.PersistentGenericSet 1[CreditoImobiliarioBB.Model.Pessoa]' to type 'System.Collections.Generic.IList 1 [CreditoImobiliarioBB.MenericBILB. Model.Pessoa]. " StackTrace Result:
at (Object, Object [], SetterCallback) in NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues ​​(Object target, Object [] values) in NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer (object, object [] - - End of internal check of the exception stack --- in NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer (object object, object []) in NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues ​​(object object, object []) .Persister.Entity.AbstractEntityPersister.SetPropertyValues ​​(Object obj, Object [] values, EntityMode entityMode) in NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate (object object, EntityKey key, IEntityPersister object logec, persister, persecutor, all log, persister, persister ntSource, the boolean value of requireImmediateIdAccess) in NHibernate.Event.Default.AbstractSaveEventListener.PerformSave (object object, object identifier, IEntityPersister persister, boolean useIdentityColumn, all object, IEventSource source, boolean value requireImmediateIdAccess) in AbstractAbouteraveaveate. object object, String entityName, object any, source IEventSource, Boolean trebuetImmediateIdAccess) in NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId (SaveOrUpdateEvent event) in NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId (SaveOrUpdateEvent event) in NHibernate.Event.Default. DefaultSaveOrUpdateEventListener.EntityIsTransient (SaveOrUpdateEvent event) in NHibe rnate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate (SaveOrUpdateEvent event) in NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate () EventInfreave. eventPlate. eventPlate.enerprevent.plen.pln.plen.plen.plen.pln.preferences ) on the FluentNHibernate.Testing.PersistenceSpecification 1.TransactionalSave(Object propertyValue) at FluentNHibernate.Testing.Values.ReferenceProperty 2.HasRegistered (PersistenceSpecification 1 specification) at FluentNHibernate.Testing.PersistenceSpecification propertyPrice 1 property, IEqualityComparer equalityComparer) at FluentNHibernate.Testing.PersistenceSpecificationExtensions.CheckReference[T](PersistenceSpecification 1 specification, Expression`1 expression, Object propertyValue, IEqualityComparer propertyComparer) in CreditoImobiliarioBB.Repository.Integration.Test.AgenciaTest.AgenciaTemVariosGere tes () in {OMMITED} .Integration.Test \ AgenciaTest.cs: line 27

Thanks.

+6
source share
1 answer

Sets do not implement IList<T> .

Define your properties as ICollection<T> .

+11
source

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


All Articles