Search code examples
c#fluent-nhibernate

Why does Fluent nHibernate not set Id of child object when using SaveOrUpdate?


Why does Fluent nHibernate not set Id of child object when using SaveOrUpdate?

I got the following fluent nHibernate domain and mapping classes (reduced for this question):

public class Error
{
    public virtual long Id { get; set; }
    
    public virtual string Message { get; set; }
    
    public virtual Error InnerException { get; set; }
    

}


class ErrorMap : ClassMap<Error>
{
    public ErrorMap()
    {
        Id(x => x.Id);
        
        Map(x => x.Message);
        
        HasOne(x => x.InnerException).Cascade.All();
        
    }

}

I use this code to save a Error-object to the database:

using (var transaction = m_protokollSession.BeginTransaction())
{

    Error err = new Error()
    {
        Message = "Outher",
        InnerException = new Error()
        {
            Message = "Inner",
        }
    };

    m_protokollSession.SaveOrUpdate(err);

    transaction.Commit();

}

The problem is, that the "InnerException_id"-Column of the "Outher"-Row is null. But it should have the Id of the "Inner" row.


Solution

  • Ok. I found it out by myself:

    I had to use:

            References(x => x.InnerException).Cascade.All();
    

    in the mapping class (ErrorMap)