Cant insert row with auto-increment key via FluentNhibernate

Posted by Jeff Shattock on Stack Overflow See other posts from Stack Overflow or by Jeff Shattock
Published on 2010-04-25T02:28:29Z Indexed on 2010/04/25 2:33 UTC
Read the original article Hit count: 954

I'm getting started with Fluent NHibernate, and NHibernate in general. I'm trying to do something that I feel is pretty basic, but I cant quite get it to work. I'm trying to add a new entry to a simple table.

Here's the Entity class.

public class Product
{
    public Product()
     {
        id = 0;
    }
    public virtual int id {get; set;}
    public virtual string description { get; set; }
}

Here's its mapping.

public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Id(p => p.id).GeneratedBy.Identity().UnsavedValue(0);
        Map(p => p.description);
    }
}

I've tried that with and without the additional calls after Id().

And the insert code:

var p = new Product() { description = "Apples" };
using (var s = _sf.CreateSession())
{
    s.Save(new_product);
    s.Flush();
}

where _sf is a properly configured SessionSource.

When I execute this code, I get: NHibernate.AssertionFailure : null identifier, which makes sense based on the SQL that NHibernate is executing: INSERT INTO "Product" (description) VALUES (@p0);@p0 = 'Apples'

It doesnt seem to be trying to set the Id field, which seems ok (on its face) since the DB should generate that. But its not, I think. The DB schema is autogenerated by FNH:

var config = Fluently.Configure().Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString(@"Data Source=Database1.sdf"));
var SessionSource = new SessionSource(config.BuildConfiguration().Properties, new ModelMappings());
var Session = SessionSource.CreateSession();
SessionSource.BuildSchema(Session);
CreateInitialData(Session);
Session.Flush();
Session.Clear();

I'm sure to be doing tons of things wrong, but whats the one thats causing this error?

© Stack Overflow or respective owner

Related posts about fluent-nhibernate

Related posts about nhibernate