让流畅的NHibernate与SQLite一起使用

Cra*_*lla 10 sqlite nhibernate system.data.sqlite fluent-nhibernate

我确信有一些简单的事情我还没有完成,但我想让Fluent NHibernate在我的机器上使用Sqlite.

我使用NuGet下载流畅的nhibernate并添加了以下实体和映射:

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

然后,在开始使用流畅的指南之后,我将以下代码添加到Windows Command项目中:

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

最后我使用NuGet添加了Sqlite dll ..但是在尝试运行程序时遇到以下错误:

最高例外:

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
Run Code Online (Sandbox Code Playgroud)

下一个例外:

Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=3.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4.
Run Code Online (Sandbox Code Playgroud)

最内容的例外:

Unable to find the requested .Net Framework Data Provider.  It may not be installed.
Run Code Online (Sandbox Code Playgroud)

这是它试图创建会话工厂的时候.

有人能帮忙吗?我正在运行32位机器?

谢谢

戴夫

reb*_*ard 12

你需要两件事:

  1. System.Data.SQLite在您的项目中引用.
  2. 包括sqlite3.dll,但你也无法添加对sqlite3.dll的引用,因为它是一个非托管的dll.只需将其作为元素添加到解决方案中,并将其设置为复制到输出目录.


Emi*_*nem 5

经过初步调查,我认为这是因为我的System.Data.SQLite程序集当时没有加载到内存中,所以我包含了预加载system.Data.SQLite程序集的代码.但是,在运行应用程序时出现了真正的错误:

混合模式组件被构建针对运行时的版本"V2.0.50727",并且不能在没有额外的配置信息的4.0运行时加载.

为了解决这个问题,我将app.config更改为如下所示:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)

重要的一点是useLegacyV2RuntimeActivationPolicy ="true"