C# Sqlite“;没有这样的表格”;保存对象时

C# Sqlite“;没有这样的表格”;保存对象时,c#,nhibernate,sqlite,fluent-nhibernate,C#,Nhibernate,Sqlite,Fluent Nhibernate,我正在尝试将对象插入SQLite InMembory数据库,如下所示: private void button1_Click(object sender, EventArgs e) { var sessionFactory = CreateSessionFactory(); using (var session = sessionFactory.OpenSession()) { Person p = new Pers

我正在尝试将对象插入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();
    }

我做错了什么?提前感谢。

您需要在发送任何请求之前创建表结构。一种方法是使用
NHibernate.Tool.hbm2ddl.SchemaExport
。你可以看一看。另一种方法是手动执行,即
创建表Person…
。当然,
SchemaExport
的优点是,如果修改映射,它将自动反映在生成的数据库模式上。

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


来源:

我知道这是一篇老文章

我也面临同样的问题,实际上我写的模式导出就是这样。但例外情况仍然存在

问题是您需要使用打开的会话来执行模式导出。因此,您需要修改您的配置

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();
我希望它能帮助其他面临同样问题的人

注意


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

谢谢,我在家时会看这个,我会回答。
SchemaExport.Create
不允许刷新会话(
.Flush()
)<因此,根据ktutnik的回答,code>SchemaExport.Execute是这个实例中更好的选择。谢谢,这刚刚帮我省去了一大堆痛苦!UsingFile()-works。InMemory()-不工作。啊!然后我找到了你的帖子,它一直是我的培根。感谢您,这适用于最新版本的NHibernate(4.1.0.4000)和FluentNHibernate(2.0.3.0)
   return Fluently.Configure()
   .Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
   .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Person>())
   .ExposeConfiguration(BuildSchema)
    .BuildSessionFactory();
private void BuildSchema(Configuration cfg)
{
  new SchemaExport(cfg)
    .Create(false, true);
}
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();
ISession session = GetSessionFactory().OpenSession();
//the key point is pass your session.Connection here
new SchemaExport(config).Execute(true, true, false, session.Connection, null);