微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使Fluent NHibernate与SQLite一起使用

如何解决使Fluent NHibernate与SQLite一起使用

| 我确定有一些简单的事情我还没做完,但是我想让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\");
        }
}
然后,按照流利的入门指南,我将以下代码添加到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);
    }
}
最后,我使用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位计算机? 谢谢 戴夫     

解决方法

您需要两件事: 在您的项目中参考ѭ5。 包括
sqlite3.dll
,但您也无法添加对sqlite3.dll的引用,因为它是非托管dll。只需将其作为元素添加到解决方案中,然后将其设置为复制到输出目录即可。     ,您需要Sqlite的.NET提供程序。在此处下载它http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki     ,经过初步调查,我认为是因为当时没有将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 \”     ,我今天遇到了同样的问题,花了一个小时寻找解决方案。无论如何,我在这里找到一个: http://thelongwayback.wordpress.com/2009/11/02/fluent-nhibernate-sqlite-problem-the-idbcommand-and-idbconnection-implementation-in-the-assembly-system-data-sqlite-could-not-被发现/ 基本上,您必须从此处使用旧版本的SQLite(v1.0.60): http://sourceforge.net/projects/sqlite-dotnet2/files/ 另一方面,我昨天在装有VS2010 SP1的计算机上使用了相同的代码。没有SP1的计算机今天不会运行相同的代码。如果有人有机会测试此版本,即通过安装VS2010 SP1,请告诉我结果。     ,以上任何一种解决方案,或者我在Internet上找不到的其他解决方案,都没有为我工作...直到... 我必须同时安装x64和x86版本的SQLite(按照Vadim的链接:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki),订购。 最初,我在64位版本之前安装了32位,但没有修复任何问题。一时兴起,我将它们都卸载了,然后以相反的顺序重新安装了它们。现在可以了! 我已经在2台不同的计算机上对此进行了测试,并验证了这对他们两人都可以解决。古怪,但有效。     ,我也遇到了这个问题。 注意,入门示例项目针对.net framework 3.5,而我创建的示例项目(锻炼项目)针对.net framework 4.0客户端配置文件(默认为vs2010)。 我将目标版本更改为3.5,并且可以进行锻炼项目。     ,就我而言,它可以通过NuGet安装System.Data.SqLite。 因此,我打开了“工具/ NuGet程序包管理器/程序包管理器控制台”并输入:
Install-Package System.Data.SqLite
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?