2010-08-12 46 views
2

晚安人,功能NHibernate與SQL緊湊:架構問題

我一直在使用功能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技術,你會認爲這足以暗示表格模式....每次測試運行。

任何指導的幫助非常感謝。 謝謝你的時間。

+0

請張貼整個堆棧跟蹤 – 2010-08-12 21:53:23

回答

0

好的,對不起,我問這樣一個問題並沒有發佈堆棧跟蹤是愚蠢的。可悲的是,因爲我剛開始這個項目,所以我只能通過單元測試運行我的代碼,因此我感覺從Nunit獲得的關於測試失敗的信息不如堆棧跟蹤那麼好。

從NUnit的GUI '文本輸出' 窗口

***** SolutionName.Testing.DataAccess.Unit.Testing_BaseDAO.Create_CanCreateBaseDAO 

21:34:32929 ERROR [TestRunnerThread] SchemaUpdate工具[(空)] - 無法完成架構更新 System.NotSupportedException:指定方法不受支持。

在System.Data.Common.DbConnection.GetSchema(字符串集合名,字符串[] restrictionValues)

在NHibernate.Dialect.Schema.AbstractDataBaseSchema。的getTables(字符串目錄,字符串schemaPattern,字符串tableNamePattern,字符串[]類型)

在NHibernate.Tool.hbm2ddl.DatabaseMetadata.GetTableMetadata(字符串名稱,字符串模式,字符串目錄,布爾isQuoted)

從NUnit的GUI '錯誤和故障' 窗口

at SolutionName.DataAccess.ConnectionProviders.NHibernateConnection.get_Session() in SolutionFolder.SolutionName.DataAccess\ConnectionProviders\NHibernateConnection.cs:line 25 

在SolutionName.DataAccess.ConnectionProviders.NHibernateConnection.Create [TEntity](TEntity objectToCreate)在SolutionFolder \ SolutionName.DataAccess \ ConnectionProviders \ NHibernateConnection.cs:線91 在SolutionFolder \ SolutionName.DataAccess.BaseDAO`1.Cre在SolutionFolder \ SolutionName.DataAccess \ BaseDAO.cs中的ate(T objectToCreate):行31 at SolutionName.Testing.DataAccess.Unit.Testing_BaseDAO.Function_CanCreateDomainObject()in SolutionFolder \ SolutionName.Testing \ DataAccess \ Unit \ Testing_BaseDAO.cs:line 46

在NHibernate.Cfg.Configuration.GenerateSchemaUpdateScript(方言方言,DatabaseMetaData的DatabaseMetaData的 在NHibernate.Tool.hbm2ddl.SchemaUpdate.Execute(Action`1 scriptAction,布爾doUpdate)