2013-11-27 100 views
0

我現在有這些地圖功能NHibernate PersistentSpecification測試失敗

public class CountryMap : ClassMap<Country> 
{ 
    public CountryMap() 
    { 
     Table("tblCountry"); 
     Id(x => x.Id, "intCountryId"); 
     Map(x => x.Name, "strCountryName"); 
     HasMany(x => x.FlagImages) 
      .Table("tblImage") 
      .KeyColumn("intRelId") 
      .Where("intObjId=29") 
      .Not.Cascade.All(); 
    } 
} 

public class ImageMap : ClassMap<Image> 
{ 
    public ImageMap() 
    { 
     Table("tblImage"); 
     Id(x => x.Id, "intImgId"); 
     Map(x => x.ObjId, "intObjId"); 
     Map(x => x.RelId, "intRelId"); 
     Map(x => x.ImageName, "strImage"); 
    } 
} 

及以下規格持續測試:

 PersistenceSpecification<Image>(session)() 
      .CheckProperty(r => r.Id, 1) 
      .CheckProperty(r => r.ImageName, "ss") 
      .CheckProperty(r => r.ObjId, (int)ObjectType.Country) 
      .CheckProperty(r => r.RelId, 102) 
      .VerifyTheMappings(); 

有點的Image測試失敗。

的失敗消息是 Assert.AreEqual failed. Expected:<(null)>. Actual:<NHibernate.Exceptions.GenericADOException: could not insert: [HansaCrew.Models.Images.Image][SQL: INSERT INTO tblImage (intObjId, intRelId, strImage) VALUES (?, ?, ?); select last_insert_rowid()] ---> System.Data.SQLite.SQLiteException: constraint failed foreign key constraint failed

我已經檢查了2天,並沒有找到原因。任何幫助?

回答

0

好的,我明白了。

這段代碼的問題就出在這裏

 HasMany(x => x.FlagImages) 
     .Table("tblImage") 
     .KeyColumn("intRelId") 
     .Where("intObjId=29") 
     .Not.Cascade.All(); 

NHibernate的自動創建tblImage的,裏面有很多的intRelId S的不是在intCountryId在表tblCountry列外鍵約束。這就是爲什麼測試失敗說外鍵約束失敗。

解決這個問題的辦法是在數據庫中創建一個view,然後view映射到Image模型