AssertionFailure: "null identifier" - FluentNH + SQLServerCE
Asked Answered
S

2

14

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
    {
        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())
                        .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);
            }
        }
    }
}
Spitler answered 2/3, 2010 at 8:31 Comment(1)
ust out of interest - what was the solution, option 1 or 2 (or another)? :-)Aksel
P
28

There's a "bug" when using NHibernate with SQL CE identity columns. There are two workarounds that I know of:

1 - Set the connection.release_mode property to on_close in configuration:

mySessionFactory = Fluently.Configure()
    .Database(MsSqlCeConfiguration.Standard
    .ConnectionString("Data Source=MyDB.sdf"))
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf())
    .ExposeConfiguration(BuildSchema)
    .ExposeConfiguration(x => x.SetProperty("connection.release_mode", "on_close"))
    .BuildSessionFactory();

2 - Perform inserts in a transaction:

    using (ISession session = DB.OpenSession())
    using (ITransaction txn = session.BeginTransaction())
    {
        session.Save(employee);
        txn.Commit();
    }

I realize the question is more than a month old but I hope this answer is still of use to you.

Pokpoke answered 6/4, 2010 at 14:1 Comment(3)
Just out of interest - what was the solution, option 1 or 2 (or another)? :-)Aksel
I have the same problem. When I use the option 1, then insert works great, but update doesn't work at all. When I use the option 2, then both insert and update work as expected, but execution time is 2x slower.Goatfish
It is best practice to always wrap all database interactions in a transaction when using NHibernate anyways. So I would only ever recommend option 2.Go
K
1

Is is still the true. Even first call to SELECT @@IDENTITY returns corect value, but seconds returns null

I use

*connection.driver_class = NHibernate.Driver.SqlServerCeDriver dialect: NHibernate.Dialect.MsSqlCeDialect*

After adding property *connection.release_mode: on_close* to my NHIbernate configuration it works

Kyte answered 30/10, 2011 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.