Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 908923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:47:38+00:00 2026-05-15T16:47:38+00:00

I’ve used Fluent NH in some projects but I’m having some problems with using

  • 0

I’ve used Fluent NH in some projects but I’m having some problems with using the PersistenceSpecification class for testing a collection mapping. Here’s the code for my classes (I’m just putting here the collection definition):

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

And here’s the mappings (only the important things):

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

As you can see, Interncao objects are added through the ModificaEstado method which ensures that Ocorrencia reference on Intervencao “points” to a reference of Ocorrencia. Now, how do I test this relationship with the PersistenceSpecification object? I’ve ended up with the following code:

[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();
    }
}
}

Since IdOcorrencia is defined as an external key in table Intervencoes, the previous code fails because it tries to insert the intervencoes list with IdOcorrencia set to null. If I remove the external key, then the test works fine, but I believe that I shouldn’t be doing that.

I’m probably doing something wrong but I’m not sure on what that is. So, can anyone be kind enough and give me a hint on how to solve this?

thanks guys.
Luis

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:47:39+00:00Added an answer on May 15, 2026 at 4:47 pm

    The problem was that I was using an old version of the fluent nhibernate. recente versions have overrides which let you solve this kind of problem:

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.