ho una classe di dominio:Impossibile eseguire il cast 'NHibernate.Collection.Generic.PersistentGenericSet`1 a System.Collections.Generic.IList`1
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; }
}
Con questa convenzione (definita come stabilito AsSet
)
public class AgenciaConvention : IAutoMappingOverride<Agencia>
{
public void Override(AutoMapping<Agencia> mapping)
{
mapping.HasManyToMany(a => a.Gerentes).Cascade.AllDeleteOrphan().AsSet().Not.Inverse();
}
}
Quando ho eseguito questo 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
}
Come posso verificare questa lista?
The collection should be
AsSet
, it necessary that the Parent and Children fields are PK, FK
completa Errore:
Nome test: AgenciaTemVariosGerentes prova FullName: {} OMMITED .Integration.Test.AgenciaTest.AgenciaTemVariosGerentes Test origine: {} OMMITED .Integration.Test \ AgenciaTest.cs : linea 22 test Risultato: Impossibile prova Durata: 0: 00: 02,4093555
messaggio Risultato: metodo di prova {} OMMITED .Integration.Test.AgenciaTest.Agen ciaTemVariosGerentes ha gettato un'eccezione: NHibernate.PropertyAccessException: Cast non valido (controlla la corrispondenza per mancata corrispondenza dei tipi di proprietà); setter di CreditoImobiliarioBB.Model.Regional ---> System.InvalidCastException: impossibile eseguire il cast dell'oggetto di tipo 'NHibernate.Collection.Generic.PersistentGenericSet 1[CreditoImobiliarioBB.Model.Pessoa]' to type 'System.Collections.Generic.IList
1 [CreditoImobiliarioBB.Model.Pessoa]'. Risultato StackTrace:
a (oggetto, oggetto [], SetterCallback) a NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues (target oggetto, oggetto [valori]) a NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer (entità Object , Valori dell'oggetto []) --- Fine della traccia di stack delle eccezioni interne --- in NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer (entità dell'oggetto, valori dell'oggetto []) in NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues (Oggetto Object, Object [] values) in NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues (Object object, Object [] values, EntityMode entityMode) in NHibernate.Event.Default.AbstractSav eEventListener.PerformSaveOrReplicate (entità Object, chiave EntityKey, IEntityPersister persister, booleano useIdentityColumn, oggetto qualsiasi cosa, fonte IEventSource, booleano requiresImmediateIdAccess) a NHibernate.Event.Default.AbstractSaveEventListener.PerformSave (entità Object, oggetto id, IEntityPersister persister, booleano useIdentityColumn, Object nulla, IEventSource fonte, booleano requiresImmediateIdAccess) a NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId (entità Object, String entityName, oggetto qualsiasi cosa, fonte IEventSource, booleano requiresImmediateIdAccess) a NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId (evento SaveOrUpdateEvent) presso NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId (evento SaveOrUpdateEvent) presso NHibernate.Eve nt.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient (evento SaveOrUpdateEvent) a NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate (evento SaveOrUpdateEvent) a NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate (evento SaveOrUpdateEvent) a NHibernate.Impl.SessionImpl.FireSave (Evento SaveOrUpdateEvent) presso NHibernate.Impl.SessionImpl.Save (oggetto Object) presso FluentNHibernate.Testing.PersistenceSpecification 1.TransactionalSave(Object propertyValue) at FluentNHibernate.Testing.Values.ReferenceProperty
2.HasRegistered (PersistenceSpecification 1 specification) at FluentNHibernate.Testing.PersistenceSpecification
1.RegisterCheckedProperty (Proprietà 1 property, IEqualityComparer equalityComparer) at FluentNHibernate.Testing.PersistenceSpecificationExtensions.CheckReference[T](PersistenceSpecification
1 spec, espressione Expression`1, oggetto propertyValue, IEqualityComparer propertyComparer) a CreditoImobiliarioBB.Repository.Integration.Test.AgenciaTest.AgenciaTemVariosGerentes() in {} OMMITED .Integration. test \ AgenciaTest.cs:. linea 27
Grazie