Vik*_*ose 3 c# nhibernate fluent-nhibernate sql-server-ce
当我使用此代码使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
我有这样的例外
创建SessionFactory时使用了无效或不完整的配置.检查PotentialReasons集合,以及InnerException以获取更多详细信息.
内在的例外是
找不到程序集System.Data.SqlServerCe中的IDbCommand和IDbConnection实现.确保程序集System.Data.SqlServerCe位于应用程序目录或全局程序集缓存中.如果程序集位于GAC中,请使用应用程序配置文件中的元素指定程序集的全名.
帮助解决这个问题.
实际上问题是,在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>
Run Code Online (Sandbox Code Playgroud)