首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fluent NHibernate:使用Fluent测试集合映射

Fluent NHibernate:使用Fluent测试集合映射
EN

Stack Overflow用户
提问于 2010-07-05 21:08:58
回答 1查看 694关注 0票数 0

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

代码语言:javascript
复制
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; }
}

下面是映射(只有重要的部分):

代码语言:javascript
复制
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对象测试这种关系呢?我最终得到了以下代码:

代码语言:javascript
复制
[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的被插入者列表。如果我删除了外键,那么测试就可以正常工作,但我认为我不应该这样做。

我可能做错了什么,但我不确定是什么。那么,有没有人能好心给我一个提示,告诉我如何解决这个问题?

谢谢你们。路易斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-06 16:12:00

问题是我使用的是一个旧版本的流畅的nhibernate。最近的版本有覆盖,可以让你解决这类问题:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3179691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档