0

我在一些项目中使用了 Fluent NH,但在使用 PersistenceSpecification 类测试集合映射时遇到了一些问题。这是我的课程的代码(我只是把集合定义放在这里):

public class Ocorrencia : EntityWithAction, IHasAssignedId<Int32> {     
  private IList<Intervencao> _intervencoes = new List<Intervencao>();
  public IEnumerable<Intervencao> Intervencoes {
   get{
   return new ReadOnlyCollection<Intervencao>( _intervencoes );
  }
 set {
   _intervencoes = new List<Intervencao>( value );
   Contract.Assume(_intervencoes != null);
 }
}   
public void ModificaEstado(Intervencao intervencao ){
 //some checks removed
 intervencao.Ocorrencia = this;
 _intervencoes.Add(intervencao);
}
//more code removed

}
public class Intervencao : EntityWithAction, IHasAssignedDomainAction {
//other code remove
internal Ocorrencia Ocorrencia { get; set; }
}

这是映射(只有重要的事情):

public class IntervencaoMapping: ClassMap<Intervencao> {
public IntervencaoMapping()
{            
    WithTable("Intervencoes");
    Not.LazyLoad();
    Id(intervencao => intervencao.Id)
        .ColumnName("IdIntervencoes")
        .WithUnsavedValue(0)
        .SetGeneratorClass("identity");
    Map(intervencao => intervencao.Guid, "Guid")
        .Not.Nullable();
    Version(ent => ent.Version)
       .ColumnName("Version");
    References(ent => ent.Action, "IdAccao")
        .Cascade
        .SaveUpdate();
    Map(intervencao => intervencao.TipoEstado, "TipoEstado")
        .CustomTypeIs(typeof (TipoEstado))
        .CustomSqlTypeIs("integer");
    Map(intervencao => intervencao.Observacoes, "Observacoes");
    References(intervencao => intervencao.Ocorrencia, "IdOcorrencias")
           .Not.LazyLoad();
}
}
public class OcorrenciaMapping: ClassMap<Sra.Ocorrencias.Ocorrencia> {
public OcorrenciaMapping()
{            
    WithTable("Ocorrencias");
    Not.LazyLoad();
    Id(ocorrencia => ocorrencia.Id)
        .ColumnName("IdOcorrencias")
        .WithUnsavedValue(0)
        .SetGeneratorClass("identity");
    Map(ocorrencia => ocorrencia.Guid, "Guid")
        .Not.Nullable();
    Version(ocorrencia => ocorrencia.Version)
        .ColumnName("Version");
    Map(ocorrencia => ocorrencia.Descricao)
        .ColumnName("Descricao");
    Map(ocorrencia => ocorrencia.Nif, "Nif")
        .Not.Nullable();
    Map(ocorrencia => ocorrencia.TipoOcorrencia, "TipoOcorrencia")
         .CustomTypeIs(typeof(TipoOcorrencia))
        .CustomSqlTypeIs("integer");
    Map(ocorrencia => ocorrencia.BalcaoEntrada, "Balcao")
        .CustomTypeIs(typeof(TipoBalcao))
        .CustomSqlTypeIs("integer")
        .Not.Nullable();

    References(ocorrencia => ocorrencia.Organismo, "IdOrganismos")
        .Cascade.None()
        .Not.Nullable();
    HasMany(ocorrencia => ocorrencia.Intervencoes)
            .Access.AsCamelCaseField(Prefix.Underscore)
            .AsBag()
            .Cascade
            .All()
            .KeyColumnNames.Add("IdOcorrencias")
            .Not.LazyLoad();
}
}

如您所见,Interncao 对象是通过 ModificaEstado 方法添加的,该方法确保 Intervencao 上的 Ocorrencia 引用“指向”Ocorrencia 的引用。现在,如何使用 PersistenceSpecification 对象测试这种关系?我最终得到了以下代码:

[Test]
public void Test() {
using (var session = _factory.GetSession()) {
    using (var tran = session.BeginTransaction()) {

        var accao = CreateAction();
        session.Save(accao);

        var organismo = CreateOrganismo();
        session.Save(organismo);

        var intervencao = CreateIntervencao();
        ((IHasAssignedDomainAction)intervencao).SetActionTo(accao);
        var intervencoes = new List<Intervencao> {intervencao};

        new PersistenceSpecification<Ocorrencia>(session)
            .CheckProperty(e => e.Nif, _nif)
            .CheckProperty( e =>e.Organismo, organismo)
            .CheckProperty( e => e.Descricao, _descricao)
            .CheckProperty( e => e.TipoOcorrencia, TipoOcorrencia.Processo)
            .CheckList( e => e.Intervencoes, intervencoes)
            .VerifyTheMappings());

            tran.Rollback();
    }
}
}

由于 IdOcorrencia 被定义为表 Intervencoes 中的外部键,因此前面的代码失败,因为它尝试插入 IdOcorrencia 设置为 null 的 intervencoes 列表。如果我删除外部密钥,那么测试工作正常,但我相信我不应该这样做。

我可能做错了什么,但我不确定那是什么。那么,任何人都可以足够友善并给我一个关于如何解决这个问题的提示吗?

多谢你们。路易斯

4

1 回答 1

0

问题是我使用的是流利的 nhibernate 的旧版本。最近的版本具有覆盖,可让您解决此类问题:

http://www.mail-archive.com/fluent-nhibernate@googlegroups.com/msg06121.html

于 2010-07-06T08:11:59.707 回答