views:

411

answers:

3

Hello. 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.

+3  A: 

You need to create the table structures before sending any requests. One way of doing is to use NHibernate.Tool.hbm2ddl.SchemaExport. You may take a look at this example. Another way is to do it manually i.e. CREATE TABLE Person .... Of course the advantage of SchemaExport is that if you modify your mappings it will automatically reflect on the generated database schema.

Darin Dimitrov
+1 yes see NHibernate.Tool.hbm2ddl.SchemaExport
dotjoe
+4  A: 

As Darin Dimitrov said, you need to export the schema. Luckily, there's a nice way to do it using Fluent NH :)

   return Fluently.Configure()
   .Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
   .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Person>())
   .ExposeConfiguration(BuildSchema)
    .BuildSessionFactory();

... where BuildSchema is a method:

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

Source: http://wiki.fluentnhibernate.org/Schema_generation

Mark Simpson
Thanks i will look at this when I'm at home and I will answer.
dario
+2  A: 

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

ktutnik