Dafydd Giddins
Dafydd Giddins

Reputation: 2316

FluentNhibernate not creating an Id column as an identity with SQL CE 4.0

I have am trying to create some integration tests with no external database dependencies so am using SQL CE 4.0 and trying to configure it with FluentNhibernate.

I have gotten so far as having the tests create the database file each time it runs, then create the table that i want to use but it never sets the Id column to and identity specification. When i then try and insert into the table with Nhibernate it complains about a null identity column.

This is my mapping defenition and data class:

public class AffiliateApplicationRecord
{
    public virtual int Id { get; private set; }
    public virtual string CompanyName { get; set; }
    public virtual string Email { get; set; }
    public virtual string DomainName { get; set; }
    public virtual DateTime DateReceived { get; set; }

    public AffiliateApplicationRecord()
    {
        DateReceived = DateTime.Now;
    }
}

public class AffiliateApplicationRecordMap : ClassMap<AffiliateApplicationRecord>
{
    public AffiliateApplicationRecordMap()
    {
        Table("Partner");
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.CompanyName, "Name");
        Map(x => x.DomainName, "Domain");
        Map(x => x.Email);
        Map(x => x.DateReceived, "TimeStampCreated");
    }
}

And these are the methods that configure the Session Factory which i call in my repositories to get a session factory to run queries/insert data.

EDIT: Added Dialect configuration...

private ISessionFactory CreateSessionFactory()
    {

        var config = Fluently.Configure()
            .Database(
                MsSqlCeConfiguration.Standard
                    .Dialect<MsSqlCe40Dialect>()
                    .ConnectionString("Data Source=DatabaseFileName.sdf"))
            .Mappings(m => m.FluentMappings.AddFromAssembly(typeof(AffiliateApplicationRecord).Assembly))
            .ExposeConfiguration(BuildSchema)
            .BuildConfiguration()

        return config.BuildSessionFactory();
    }

    private static void BuildSchema(Configuration config)
    {
        new SchemaExport(config).Create(false, true);
    }

I'd really appreciate help with this. I am worried that either Fluent won't do this for me or that SQL CE won't work.

Thanks in advance for the help.

Upvotes: 2

Views: 1354

Answers (2)

Dafydd Giddins
Dafydd Giddins

Reputation: 2316

After using the SQLCe toolbox i realised that the table was being created with an identity column correctly and that the problem i was seeing (AssertionFailure: “null identifier”) was actually caused by a bug in SQL CE described here...

AssertionFailure: "null identifier" - FluentNH + SQLServerCE

Many Thanks SO

Upvotes: 3

Nathan Fisher
Nathan Fisher

Reputation: 7941

try using the MsSqlCe40Dialect instead of the MsSqlCeDialect.

Upvotes: 0

Related Questions