让 Fluent NHibernate 与 SQLite 一起使用

发布于 2024-10-31 23:44:51 字数 2469 浏览 0 评论 0原文

我确信有一些简单的事情我还没有做过,但我正在尝试让 Fluent NHibernate 在我的机器上与 Sqlite 一起使用。

我使用 NuGet 下载 Fluent 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");
        }
}

然后按照 Fluent 入门指南,我将以下代码添加到 Windows 命令项目中:

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);
    }
}

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

顶部异常:

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

下一个异常:

Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=3.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4.

最内部异常:

Unable to find the requested .Net Framework Data Provider.  It may not be installed.

这是当它尝试创建会话工厂时。

谁能帮忙解决这个问题吗?我运行的是 32 位机器?

谢谢

戴夫

I'm sure there is something simple I've not done but I'm trying to get Fluent NHibernate to work with Sqlite on my machine.

I used NuGet to download fluent nhibernate and added the following entity and mapping:

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");
        }
}

Then following the getting started with fluent guide I added the following code to a Windows Command project:

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);
    }
}

Finally I added the Sqlite dll using NuGet.. however I'm getting the following error when trying to run the program:

Top Exception:

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

Next Exception:

Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=3.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4.

Inner most exception:

Unable to find the requested .Net Framework Data Provider.  It may not be installed.

This is when it's trying to create the session factory.

Can anyone help with this? I'm running a 32 bit machine?

Thanks

Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

盗梦空间 2024-11-07 23:44:52

我今天遇到了同样的问题,花了一个小时寻找解决方案。无论如何,我在这里找到了一个:

http://thelongwayback.wordpress.com/2009/11/02/ Fluent-nhibernate-sqlite-problem-the-idbcommand -and-idbconnection-implementation-in-the-assemble-system-data-sqlite-could-not-be-found/

基本上,您必须使用旧版本的 SQLite (v1.0.60)这里:
http://sourceforge.net/projects/sqlite-dotnet2/files/

在另一个请注意,我昨天在一台装有 VS2010 SP1 的机器上运行了相同的代码。如今,相同的代码无法在没有 SP1 的计算机上运行。如果有人有机会测试这个,即通过安装 VS2010 SP1,请告诉我结果。

I was having the same problem today, and spend an hour searching for a solution. Anyway, I found one here:

http://thelongwayback.wordpress.com/2009/11/02/fluent-nhibernate-sqlite-problem-the-idbcommand-and-idbconnection-implementation-in-the-assembly-system-data-sqlite-could-not-be-found/

Basically, you'll have to use an older version of SQLite (v1.0.60) from here:
http://sourceforge.net/projects/sqlite-dotnet2/files/

On another note, I got the same code working yesterday on a machine with VS2010 SP1. The same code wouldn't run today on a machine without SP1. If anyone gets a chance to test this one, i.e., by installing VS2010 SP1, let me know the outcome please.

风向决定发型 2024-11-07 23:44:52

上述解决方案或我在互联网上找到的任何其他解决方案都不适合我...直到...

我必须安装 SQLite 的 x64 和 x86 版本(根据 Vadim 的链接:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki),具体按照该顺序。

最初,我在64位版本之前安装了32位,这没有修复任何问题。一时兴起,我将它们都卸载并以相反的顺序重新安装。现在可以了!

我已经在两台不同的机器上对此进行了测试,并验证了这对于两台机器都是修复的。古怪,但有效。

None of the above solutions, or any else I could find on the Internet, worked for me... until...

I had to install both the x64 and x86 versions of SQLite (per Vadim's link: http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki), specifically in that order.

Initially, I installed the 32-bit before the 64-bit version, which did not fix anything. On a whim, I uninstalled them both and reinstalled them in the opposite order. Now it works!

I've tested this on 2 different machines and verified this was the fix for both of them. Wacky, but effective.

£冰雨忧蓝° 2024-11-07 23:44:52

我也遇到这个问题了
注意到入门示例项目以 .net framework 3.5 为目标,而我创建的项目(练习项目)则以 .net framework 4.0 客户端配置文件为目标(默认为 vs2010)。
我将目标版本更改为 3.5,并且能够进行我的练习项目。

I also encountered this problem.
Noticed that the starter sample project targets .net framework 3.5 and the one I created (exercise project) is targeting .net framework 4.0 client profile (default of vs2010).
I changed the target version to 3.5 and I was able to work on my exercise project.

乖乖 2024-11-07 23:44:52

就我而言,它可以通过 NuGet 安装 System.Data.SqLite。

所以我打开“工具/NuGet包管理器/包管理器控制台”并输入:

Install-Package System.Data.SqLite

In my case it worked to install the System.Data.SqLite via NuGet.

So I opened the "Tools/NuGet Package Manager/Package Manager console" and typed:

Install-Package System.Data.SqLite

余厌 2024-11-07 23:44:51

您需要做两件事:

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

You need two things:

  1. Reference System.Data.SQLite in your project.
  2. Include sqlite3.dll, but you also can’t add reference to sqlite3.dll, because it’s an unmanaged dll. Simply add it as element to solution and set it to copy to output directory.
‖放下 2024-11-07 23:44:51

您需要 Sqlite 的 .NET 提供程序。在这里下载它 http://system.data.sqlite。 org/index.html/doc/trunk/www/downloads.wiki

You need the .NET provider for Sqlite. Download it here http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

囚我心虐我身 2024-11-07 23:44:51

经过初步调查,我认为这是因为我的 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>

其中重要的一点是 useLegacyV2RuntimeActivationPolicy="true"

After initial investigation I thought it was because my System.Data.SQLite assembly wasn't loaded into memory at the time, so I included code to preload the system.Data.SQLite assembly. However when running the application the real error arose:

Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

to fix this I changed my app.config to look like the following:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

with the important bit being useLegacyV2RuntimeActivationPolicy="true"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文