Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite "No such table" when saving object

Im trying to insert object into SQLite InMembory database as follow:

private void button1_Click(object sender, EventArgs e)
    {
        var sessionFactory = CreateSessionFactory();
        using (var session = sessionFactory.OpenSession())
        {
            Person p = new Person { Age = 25, FirstName = "Dariusz", LastName = "Smith" };
            session.SaveOrUpdate(p);
            //transaction.Commit();
        }
    }

private static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
        .Database(
        SQLiteConfiguration.Standard.InMemory().ShowSql())

        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Person>())
        .BuildSessionFactory();
    }

But Im getting ERROR: "SQLite error\r\nno such table: Person" Just for clarification: Im using InMemory option.

I'm also using FluentNhibernate with mapping:

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        //Table("Person") doesn't resolve my problem
        Id(x => x.Id);
        Map(x => x.FirstName);
        Map(x => x.LastName);
        Map(x => x.Age);
    }
}

What im doing wrong? Thanks in advance.

like image 869
Dariusz Avatar asked Jan 19 '10 20:01

Dariusz


1 Answers

I know it is an old post,

I was faced the same problem, actually i wrote schema export exactly. But the exception is till appear.

The problem is you need to use you opened session to execute the schema export. So you need to modify your configuration.

ISessionFactory session = Fluently.Configure()
    .Database(SQLiteConfiguration.Standard.InMemory())
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MessagingDescriptorMap>())

    .ExposeConfiguration(c =>
    {
        config = c; //pass configuration to class scoped variable
    })
    .BuildSessionFactory();

once you make session by OpenSession() use that to feed your SchemaExport.Execute

ISession session = GetSessionFactory().OpenSession();
//the key point is pass your session.Connection here
new SchemaExport(config).Execute(true, true, false, session.Connection, null);

I hope it will help some body else who face the same problem.

Note

I Used NHibernate 2.1.2, Fluent NHibernate 1.1 and .Net 3.5

like image 150
ktutnik Avatar answered Oct 15 '22 16:10

ktutnik