7

我有一个域类:

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; }

}

使用此约定(定义为 set AsSet

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

当我运行这个测试时:

[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
}

如何测试此列表?

集合应该是AsSet,Parent和Children字段必须是PK,FK

完全错误:

测试名称:AgenciaTemVariosGerentes 测试全名:{OMMITED}.Integration.Test.AgenciaTest.AgenciaTemVariosGerentes 测试源:{OMMITED}.Integration.Test\AgenciaTest.cs:第 22 行测试结果:失败测试持续时间:0:00:02,4093555

结果消息:测试方法 {OMMITED}.Integration.Test.AgenciaTest.AgenciaTemVariosGerentes 抛出异常:NHibernate.PropertyAccessException: Invalid Cast(检查您的映射是否有属性类型不匹配);CreditoImobiliarioBB.Model.Regional 的设置器 ---> System.InvalidCastException:无法转换类型为“NHibernate.Collection.Generic.PersistentGenericSet 1[CreditoImobiliarioBB.Model.Pessoa]' to type 'System.Collections.Generic.IList1 [CreditoImobiliarioBB.Model.Pessoa]”的对象。结果堆栈跟踪:
at (Object , Object[] , SetterCallback ) at NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values) at NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) --- End of inner exception stack trace --- at NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) at NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues(Object entity, Object[] values) at NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values, EntityMode entityMode) at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event) at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event) at NHibernate.Impl.SessionImpl.Save(Object obj) at FluentNHibernate.Testing.PersistenceSpecification1.TransactionalSave(Object propertyValue) at FluentNHibernate.Testing.Values.ReferenceProperty2.HasRegistered(PersistenceSpecification 1 specification) at FluentNHibernate.Testing.PersistenceSpecification1.RegisterCheckedProperty(Property 1 property, IEqualityComparer equalityComparer) at FluentNHibernate.Testing.PersistenceSpecificationExtensions.CheckReference[T](PersistenceSpecification1 spec, Expression`1 expression, Object propertyValue, IEqualityComparer propertyComparer) at CreditoImobiliarioBB.Repository.Integration.Test.AgenciaTest.AgenciaTemVariosGerentes() 在 {OMMITED}.Integration.Test\AgenciaTest.cs :第 27 行

谢谢。

4

1 回答 1

11

集合不实现IList<T>

将您的属性定义为ICollection<T>

于 2013-05-25T00:28:29.073 回答