保存对象时Sqlite"没有这样的表"

Dar*_*usz 5 c# sqlite nhibernate fluent-nhibernate

我试图将对象插入SQLite InMembory数据库,如下所示:

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();
    }
Run Code Online (Sandbox Code Playgroud)

但我得到错误:"SQLite error\r\nno such table: Person" 只是为了澄清:我使用InMemory选项.

我也在使用FluentNhibernate和映射:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么?提前致谢.

ktu*_*nik 20

我知道这是一个老帖子,

我遇到了同样的问题,实际上我完全写了架构导出.但例外是直到出现.

问题是您需要使用打开的会话来执行架构导出.所以你需要修改你的配置.

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();
Run Code Online (Sandbox Code Playgroud)

一旦你通过OpenSession()使用它来进行会话来喂你的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);
Run Code Online (Sandbox Code Playgroud)

我希望它会帮助一些面临同样问题的身体.

注意

我使用了NHibernate 2.1.2,Fluent NHibernate 1.1和.Net 3.5


Dar*_*rov 5

您需要在发送任何请求之前创建表结构。一种方法是使用NHibernate.Tool.hbm2ddl.SchemaExport. 你可以看看这个例子。另一种方法是手动完成,即CREATE TABLE Person .... 当然,好处SchemaExport是如果你修改你的映射,它会自动反映在生成的数据库模式上。


Mar*_*son 5

正如 Darin Dimitrov 所说,您需要导出模式。幸运的是,使用 Fluent NH 有一个很好的方法:)

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

...其中 BuildSchema 是一种方法:

private void BuildSchema(Configuration cfg)
{
  new SchemaExport(cfg)
    .Create(false, true);
}
Run Code Online (Sandbox Code Playgroud)

来源:http : //wiki.fluentnhibernate.org/Schema_g​​eneration