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() {
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.