gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:13:00 24 4
gpt4 key购买 nike

当我使用 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 从 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>

关于c# - 无法使用 Nhibernate 为 SQL Server CE 创建架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15636779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com