【问题标题】:Can't create schema for SQL Server CE using Nhibernate无法使用 Nhibernate 为 SQL Server CE 创建架构
【发布时间】:2013-03-26 12:11:00
【问题描述】:

当我通过此代码使用 Nhibernate 为 SQL Server CE 创建架构时:

Fluently.Configure()
            .Database(MsSqlCeConfiguration.Standard
            .ConnectionString(c => c.Is("Data Source=" + file))
            .Dialect<NHibernate.Dialect.MsSqlCeDialect>()
            .Driver<NHibernate.Driver.SqlServerCeDriver>()
            .ShowSql())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateSessionFactory>())
            .ExposeConfiguration(BuildSchema)
            .BuildSessionFactory();  

private static void BuildSchema(Configuration config)
    {
       // new SchemaExport(config).Drop(false, true);
        //new SchemaExport(config).Create(true, true);

         //If DB File does not exists, create it.
        if (!File.Exists(file))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(databaseFileName));
            SqlCeEngine engine = new SqlCeEngine("Data Source="+ file);
            engine.CreateDatabase();
            // this NHibernate tool takes a configuration (with mapping info in)
            // and exports a database schema from it
            new SchemaExport(config).Execute(false, true, false);
            //FormulasDAO.AddDefaultFormulaCollection();
        }
        else
        {
            new SchemaUpdate(config).Execute(false, true);
        }
    }

我遇到了这样的异常

创建 SessionFactory 时使用了无效或不完整的配置。查看PotentialReasons 集合和InnerException 了解更多详情。

内部异常是

程序集中的 IDbCommand 和 IDbConnection 实现 找不到 System.Data.SqlServerCe。确保 程序集 System.Data.SqlServerCe 位于应用程序中 目录或全局程序集缓存中。如果装配在 GAC,在应用程序中使用元素 配置文件来指定程序集的全名。

帮助解决这个问题。

【问题讨论】:

  • 异常信息非常清楚。您是否尝试过按照它的指示进行操作?发生了什么?
  • 是的,我在 GAC 中检查过,System.Data.SqlServerCe.dll 在那里,我将复制本地设置为这个 dll 引用,没有用。

标签: c# nhibernate fluent-nhibernate sql-server-ce


【解决方案1】:

实际上问题是,在 GAC 中有 2 个版本的 dll,所以 Nhibernate 不知道需要使用哪个 dll,因为 NHibernate 从 GAC 获取 dll 只使用 dll 名称而不使用版本名称。

所以需要在AppConfig中告诉NHibernate

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="System.Data.SqlServerCe" fullName="System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
</assemblyBinding>

【讨论】:

  • 感谢您的时间和努力
猜你喜欢
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多