加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 无法使用Nhibernate为SQL Server CE创建架构

发布时间:2020-12-16 01:53:55 所属栏目:百科 来源:网络整理
导读:当我使用此代码使用Nhibernate为SQL Server CE创建架构时: Fluently.Configure() .Database(MsSqlCeConfiguration.Standard .ConnectionString(c = c.Is("Data Source=" + file)) .DialectNHibernate.Dialect.MsSqlCeDialect() .DriverNHibernate.Driver.Sq
当我使用此代码使用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);
        }
    }

我有这样的例外

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

内在的例外是

The IDbCommand and IDbConnection implementation in the assembly
System.Data.SqlServerCe could not be found. Ensure that the
assembly System.Data.SqlServerCe is located in the application
directory or in the Global Assembly Cache. If the assembly is in
the GAC,use element in the application
configuration file to specify the full name of the assembly.

帮助解决这个问题.

解决方法

实际上问题是,在GAC中有2个版本的dll所以Nhibernate不知道哪个dll需要使用,因为NHibernate使用dll名称从gAC获取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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读