Dave Alger
Dave Alger

Reputation: 359

GUID connection to Sqlite UniqueIdentifier causing query errors in NHibernate

I'm working with someone's Sql Server database and a Fluent NHibernate mapped model which I test using (my own) SQLite database.

One table is called "Trad" and contains a "TradId" column which is a GUID type and so is declared as

 public virtual Guid TradId { get; set; }

and mapped:

Id(x => x.TradId).Column("TradID").GeneratedBy.Assigned();

for the purposes of testing in my SqlLite database it is declared using the "UniqueIdentifier" type which I believe maps to a string.

TradID    UNIQUEIDENTIFIER NOT NULL collate nocase,

Generally this works fine, except when any attempt is made by NHibernate to fetch by ID value, which comes up when connecting to child records.

Nhibernate spits out:

[...] WHERE this_.TradID = @p0;@p0 = ac3e30ff-e767-440f-ab1f-0000293c5a0c [Type: Guid (0)]

But this won't work because I think it needs to be handled as a string. Is there something I should be doing with the Sqlite connection to map this differently? Or is there something else I'm missing?

========================================================================

Additional info

I have a unit test with this code:

        var session = this.openSession;
        var trad = session.Query<Trad>()
            .FetchMany(x => x.TradInventeringList)
            .ToList()[0];

        var surveys = session.Query<TradInventering>()
            .Where(x => x.Trad.TradId == trad.TradId)
            .ToList();

        Assert.Greater(surveys.Count, 0);

This code runs if I have a session connected to my SqlServer (live) database, but fails against my Sqlite database.

I'm wondering if I need to add an IPropertyConvention based convention to my Fluently.Configure.

Upvotes: 1

Views: 2218

Answers (2)

Dave Alger
Dave Alger

Reputation: 359

After spending far too much time messing around with PropertyConventions I saw this post on Hibernating Rhinos.

The problem was solved by simply re-writing the FluentNhibernate SQLiteConfiguration to:

    config =
           SQLiteConfiguration.Standard
           .ConnectionString(
           "Data Source=" + databaseFilename + ";Version=3;BinaryGuid=False")
           .ShowSql();

Setting BinaryGuid=False handles the problem. Now it works fine.

Upvotes: 4

Ricardo Peres
Ricardo Peres

Reputation: 14535

Have you tried uuid.string generator? It uses GUIDs transformed into strings.

Upvotes: 0

Related Questions