我一直在使用功能NHibernate了一段時間,我從來沒有真正挖掘過深成其功能,或在許多方面,它可以配置。在每一個項目我已經用它在我進行如下配置:
if (nhConfig == null)
{
nhConfig = new NHibernate.Cfg.Configuration();
nhConfig.Properties.Add(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, typeof(NHibernate.ByteCode.LinFu.ProxyFactoryFactory).AssemblyQualifiedName);
// This obviously changes depending on what im trying to connect to
nhConfig.Properties.Add(NHibernate.Cfg.Environment.ConnectionDriver, typeof(NHibernate.Driver.SqlServerCeDriver).AssemblyQualifiedName);
// Configuration is simply an implementation
// of an IConfiguration interface
nhConfig.Properties.Add(NHibernate.Cfg.Environment.ConnectionString, configuration.ConnectionString);
// In this instance, this resolves to
// "NHibernate.Dialect.MsSqlCeDialect"
nhConfig.Properties.Add(NHibernate.Cfg.Environment.Dialect, configuration.Dialect.AssemblyQualifiedName);
nhConfig.Properties.Add(NHibernate.Cfg.Environment.ShowSql, "true");
_sessionFactory = Fluently.Configure(nhConfig)
.Mappings(m => m.FluentMappings.AddFromAssembly(configuration.MappingAssembly))
.ExposeConfiguration(x => new SchemaUpdate(x).Execute(false, true))
.BuildSessionFactory();
}
道歉的格式混亂的代碼塊......我永遠無法弄清楚如何,一旦你在貼吧沒有去編輯代碼 激活代碼標籤。
正如我所說,在我所有的其他項目中,這個配置代碼工作正常。數據庫一直是SQLExpress數據庫(2005 || 2008),如果該表不存在於指定實體的數據庫中,則該數據庫會自動創建。
但是,這次需要SQL Compact數據庫。我已經創建了這個數據庫只需添加一個新的數據庫項目到我的程序集(TestingDB.sdf),並且實際創建了一個「客戶」表。
現在我有以下的單元測試:
[Test]
public void Function_CanCreateDomainObject()
{
// Create a fake entity that is mapped
Customer fakeCustomer = new Customer();
fakeCustomer.Name = "Function_CanCreateDomainObject";
fakeCustomer.Birthday = DateTime.Now;
// Get a DAO for communication
IBaseDAO<Customer> customerDao = Container.RequestForType <IBaseDAO<Customer>>();
// Persist the domiain object
customerDao.Create(fakeCustomer);
// Retrieve it in a new instance
Customer retrievedCustomer = customerDao.Read(fakeCustomer.Id);
// Compare values for equality
Assert.That(retrievedCustomer.Id.Equals(fakeCustomer.Id),
"Retrieved entity with Name: " + retrievedCustomer.Name + "(" + retrievedCustomer.Id + ")" +
"does not match with original entity with Name: " + fakeCustomer.Name + "(" + fakeCustomer.Id + ")");
}
不管是設計不良的「單元測試」的測試翻倒並出現以下錯誤的:
ERROR [TestRunnerThread] SchemaUpdate [(null)]- could not complete schema update
System.NotSupportedException: Specified method is not supported.
是的,我把所有的所需的程序集來自我的C:\上的SQL Compact文件夾,實際上這裏是一個奇怪的部分,在試圖找到這個錯誤的源頭時,我當然會調試單元測試,並且逐行地逐步完成這個過程。有兩次,測試已通過,並且'fakeCustomer'實體在我的數據庫中正確保存,但是,有99%的時間,它會因上述錯誤而失敗。當逐行進行測試並從Nunit GUI簡單運行時,測試已通過。
所以,看起來NHibernate不能暗示SQL Compact數據庫的表結構本身,這很奇怪,因爲我使用的是FNHibernate的ClassMap技術,你會認爲這足以暗示表格模式....每次測試運行。
任何指導的幫助非常感謝。 謝謝你的時間。
請張貼整個堆棧跟蹤 – 2010-08-12 21:53:23