2012-05-02 78 views
2

我在測試我的流利NHibernate映射NUnit錯誤。流利NHibernate一對多不能插入(外鍵約束)

NHibernate.Exceptions.GenericADOException : could not insert: Contact [...] 
----> System.Data.SqlClient.SqlException : The INSERT statement conflicted with 
the FOREIGN KEY constraint [...] with table ContactType 

編輯:改性映射時功能NHibernate試圖堅持它的孩子(ContactType),從而給了我一個外鍵衝突前,堅持在數據庫中的一個對象(聯繫)出現的問題供應商在HasMany()上使用Inverse()。還提供了我的映射測試的例子。

現在,細節。

我有一個供應商有很多聯繫(一對多)。 聯繫人與其他表具有多對一的關係,聯繫方式。請記住,我在這裏簡化模型,以便我們可以專注於這個問題。

public class SupplierMap : ClassMap<Supplier> 
{ 
     public SupplierMap() 
     { 
      Table("Supplier"); 
      LazyLoad(); 
      Id(x => x.Id).GeneratedBy.Identity().Column("id"); 
      HasMany(x => x.Contacts).KeyColumn("supplierId").Cascade.All().Inverse(); 
     } 
} 
public class ContactMap : ClassMap<Contact> 
{ 
    public ContactMap() 
    { 
     Table("Contact"); 
     LazyLoad(); 
     Id(x => x.Id).GeneratedBy.Identity().Column("id"); 
     References(x => x.ContactType).Column("contactTypeId"); 
     References(x => x.Supplier).Column("supplierId").Cascade.All(); 
    } 
} 
public class ContactTypeMap : ClassMap<ContactType> 
{ 

    public ContactTypeMap() { 
     Table("ContactType"); 
     LazyLoad(); 
     Id(x => x.Id).GeneratedBy.Identity().Column("id"); 
    } 
} 

現在,當我測試ContactMap測試運行得很好,沒有任何錯誤。但是,當我運行供應商地圖測試時,這是我得到錯誤的地方。正如預期的那樣,當我運行ContactMap測試中,我得到這樣的事情在跟蹤:

INSERT INTO ContactType [...] 
INSERT INTO Contact [...] 
SELECT [...] 
[...] 

ContactMap測試:

[Test] 
    public void CanCorrectlyMapContact() 
    { 
     new PersistenceSpecification<Contact>(Session) 
    .CheckProperty(c => c.Id, 1) 
    .CheckReference(c => c.ContactType, new ContactType() { Id = 1, Archived = false, DescriptionEn = Constants.LOREM_IPSUM_255, DescriptionFr = Constants.LOREM_IPSUM_255, NameEn = Constants.LOREM_IPSUM_50, NameFr = Constants.LOREM_IPSUM_50 }) 
    .CheckReference(c => c.Title, new Title() { Id = 1, Archived = false, NameEn = Constants.LOREM_IPSUM_255, NameFr = Constants.LOREM_IPSUM_255, SortOrder = 0}) 
    .CheckReference(c => c.Region, new Region() { Id = 1, Archived = false, NameEn = Constants.LOREM_IPSUM_128, NameFr = Constants.LOREM_IPSUM_128, SortOrder = 0 }) 
    .VerifyTheMappings(); 
    } 

對於SupplierMap考試不過,我試圖插入聯繫第一(無需插入聯繫人類型)。

SupplierMap測試:

[TestFixture] 
public class SupplierMapTest : DatabaseSetup 
{ 
    private IList<Contact> tmpList; 

    public SupplierMapTest() 
    { 
     Contact tmpContact = new Contact() 
     { 
      Id = 1, 
      FirstName = Constants.LOREM_IPSUM_128, 
      LastName = Constants.LOREM_IPSUM_128, 
      Address = Constants.LOREM_IPSUM_128, 
      City = Constants.LOREM_IPSUM_128, 
      Email = Constants.LOREM_IPSUM_128, 
      FaxNumber = Constants.PHONE_NUMBER_50, 
      PhoneNumber = Constants.PHONE_NUMBER_50, 
      PostalCode = Constants.POSTAL_CODE_10, 
      Archived = false 
     }; 
     tmpContact.ContactType = new ContactType { Id = 1, Archived = false, DescriptionEn = Constants.LOREM_IPSUM_255, DescriptionFr = Constants.LOREM_IPSUM_255, NameEn = Constants.LOREM_IPSUM_50, NameFr = Constants.LOREM_IPSUM_50 }; 
     this.tmpList = new List<Contact>(); 
     this.tmpList.Add(tmpContact); 
    } 

    /// <summary> 
    /// Verify if the maping is successfull 
    /// </summary> 
    [Test] 
    public void CanCorrectlyMapSupplier() 
    { 

     new PersistenceSpecification<Supplier>(Session) 
     .CheckProperty(x => x.Id, 1) 
     .CheckList(x => x.Contacts, this.tmpList) 
     .VerifyTheMappings(); 
    } 

這個測試結果在誤差在這篇文章的頂部。

在此先感謝您的幫助。如果需要,我還可以提供更多細節。

回答

0
HasMany(x => x.Contacts).KeyColumn("supplierId").Inverse(); 

看到here以獲取更多信息

+0

我忘了提,我也試試。開始時對我來說更有意義,但不幸的是,即使在SupplierMap側設置了Inverse(),操作鏈首先是'INSERT Contact',然後立即失敗。 – gturc