Alex Rivera | Logout

Getting Fluent NHibernate to work with SQLite

Asked 2011-04-14T15:04:24.770
10

I'm sure there is something simple I've not done but I'm trying to get Fluent NHibernate to work with Sqlite on my machine.

I used NuGet to download fluent nhibernate and added the following entity and mapping:

public class Customer
{
    public virtual string CustomerCode { get; set; }
    public virtual string Name { get; set; }
}

public class CustomerMap : ClassMap<Customer>
{
    public CustomerMap ()
        {
        Id(x => x.CustomerCode);
        Map(x => x.Name);
        Table("tblCustomer");
        }
}

Then following the getting started with fluent guide I added the following code to a Windows Command project:

class Program
{
    static void Main(string[] args)
    {

        var sessionFactory = CreateSessionFactory();

        using (var session = sessionFactory.OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {

                var customer = new Customer { CustomerCode = "123", Name = "Bob" };
                session.SaveOrUpdate(customer);
                transaction.Commit();
            }
        }
    }

    private static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
            .Database(
            SQLiteConfiguration.Standard
            .UsingFile("firstProject.db")
            )
            .Mappings(m =>
                        m.FluentMappings.AddFromAssemblyOf<Program>())
            .ExposeConfiguration(BuildSchema)
            .BuildSessionFactory();
    }

    private static void BuildSchema(Configuration config)
    {
        // delete the existing db on each run
        if (File.Exists("firstProject.db"))
            File.Delete("firstProject.db");

        // this NHibernate tool takes a configuration (with mapping info in)
        // and exports a database schema from it
        new SchemaExport(config)
          .Create(false, true);
    }
}

F

Edit
Report

1 Answer

1

None of the above solutions, or any else I could find on the Internet, worked for me... until...

I had to install both the x64 and x86 versions of SQLite (per Vadim's link: http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki), specifically in that order.

Initially, I installed the 32-bit before the 64-bit version, which did not fix anything. On a whim, I uninstalled them both and reinstalled them in the opposite order. Now it works!

I've tested this on 2 different machines and verified this was the fix for both of them. Wacky, but effective.

answered 2013-03-18T20:50:57.710

Your Answer