AssertionFailure: null identifier

Stefan's Avatar

Stefan

01 Mar, 2010 02:03 PM via web

The code fails at session.Save(employee); with AssertionFailure "null identifier". What am I doing wrong?

`using FluentNHibernate.Cfg; using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace FNHTest
{

public class Employee
{
    public virtual int Id
    {
        get;
        private set;
    }

    public virtual string Name
    {
        get;
        set;
    }

    public virtual string Surname
    {
        get;
        set;
    }
}

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Id(e => e.Id);
        Map(e => e.Name);
        Map(e => e.Surname);
    }
}

public class DB
{
    private static ISessionFactory mySessionFactory = null;

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (mySessionFactory == null)
            {
                mySessionFactory = Fluently.Configure()
                    .Database(MsSqlCeConfiguration.Standard
                                .ConnectionString("Data Source=MyDB.sdf"))
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<DB>())
                    .ExposeConfiguration(BuildSchema)
                    .BuildSessionFactory();
            }
            return mySessionFactory;
        }
    }

    private static void BuildSchema(Configuration configuration)
    {
        SchemaExport schemaExport = new SchemaExport(configuration);
        schemaExport.Execute(false, true, false);
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var employee = new Employee
        {
            Name = "John",
            Surname = "Smith"
        };

        using (ISession session = DB.OpenSession())
        {
            session.Save(employee);
        }
    }
}

}`

  1. Support Staff 2 Posted by Hudson Akridge on 02 Mar, 2010 05:14 PM

    Hudson Akridge's Avatar

    You need to protected internal the setter on the ID, or generate it in the ctor.

  2. 3 Posted by Stefan on 02 Mar, 2010 05:38 PM

    Stefan's Avatar

    Using the protected internal modifier does not solve the problem.

  3. Support Staff 4 Posted by Hudson Akridge on 02 Mar, 2010 05:44 PM

    Hudson Akridge's Avatar

    Try setting the Id(x=> x.Id).GeneratedBy().Identity(); explicitly, it should be implicitly set as the default, but always pays to be sure. protected internal is required so that NHibernate can set the ID of the entity after you save it to the database (and presumably it generates the ID for you)

    I'm not familiar with SqlCe, does it support an identity generation strategy? If it doesn't, you might have to use an alternative. You might also try with MsSql, or SqLite to see if it's an issue with the SqlCe identity support in NHibernate.

    You should also use a transaction to save the employee (I don't think that's causing this error, but it might cause you an error down the road)

  4. Support Staff 5 Posted by James Gregory on 04 Mar, 2010 01:26 PM

    James Gregory's Avatar

    Stefan, any resolution on this issue? Did Hudson's suggestions help at all?

  5. 6 Posted by Stefan on 04 Mar, 2010 01:41 PM

    Stefan's Avatar

    The property Id in the Employee class can be private and NH will handle it. The default generation strategy for the Id is OK and works.

    The problem was that I needed to use transaction. I don't know why is it causing the error, but saving the entity in a transaction solved the problem.

  6. James Gregory closed this discussion on 05 Mar, 2010 09:48 AM.

Comments are currently closed for this discussion. You can start a new one.