Fluent nhibernate Fluent NHibernate一对多无法插入(外键约束)

Fluent nhibernate Fluent NHibernate一对多无法插入(外键约束),fluent-nhibernate,nunit,one-to-many,Fluent Nhibernate,Nunit,One To Many,使用NUnit测试我的Fluent NHibernate映射时出错。当Fluent NHibernate尝试在数据库中保留一个对象(联系人),然后再保留其子对象(联系人类型)时,会出现问题,从而导致外键冲突: NHibernate.Exceptions.GenericADOException : could not insert: Contact [...] ----> System.Data.SqlClient.SqlException : The INSERT statement co

使用NUnit测试我的Fluent NHibernate映射时出错。当Fluent NHibernate尝试在数据库中保留一个对象(联系人),然后再保留其子对象(联系人类型)时,会出现问题,从而导致外键冲突:

NHibernate.Exceptions.GenericADOException : could not insert: Contact [...]
----> System.Data.SqlClient.SqlException : The INSERT statement conflicted with 
the FOREIGN KEY constraint [...] with table 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测试:

[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();
    }
[测试]
public void CanCorrectlyMapContact()
{
新PersistenceSpecification(会话)
.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})
.验证应用程序();
}
然而,对于供应商地图测试,我尝试先插入联系人(不尝试插入联系人类型

供应商地图测试:

[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();
    }
[TestFixture]
公共类SupplierMapTest:数据库设置
{
私有IList tmpList;
公共供应商地图测试()
{
联系人tmpContact=新联系人()
{
Id=1,
FirstName=Constants.LOREM_IPSUM_128,
LastName=Constants.LOREM_IPSUM_128,
地址=Constants.LOREM_IPSUM_128,
City=Constants.LOREM_IPSUM_128,
Email=Constants.LOREM_IPSUM_128,
传真号码=常数。电话号码为50,
PhoneNumber=常数。电话号码为50,
PostalCode=常量。邮政编码为10,
存档=错误
};
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=新列表();
this.tmpList.Add(tmpContact);
}
/// 
///验证映射是否成功
/// 
[测试]
public void可更正地图供应商()
{
新PersistenceSpecification(会话)
.CheckProperty(x=>x.Id,1)
.检查表(x=>x.联系人,此.tmpList)
.验证应用程序();
}
此测试导致此帖子顶部出现错误

提前感谢您的帮助。如果需要,我还可以提供更多细节

HasMany(x => x.Contacts).KeyColumn("supplierId").Inverse();
有关更多信息,请参阅


有关更多信息,请参见

我忘了提到我曾经尝试过。这对我来说一开始更有意义,但不幸的是,即使使用了reverse()设置在SupplierMap端,操作链是“插入联系人”,并立即失败。我忘了提到我曾经尝试过。这对我来说一开始更有意义,但不幸的是,即使在SupplierMap端设置了reverse(),操作链也是“插入联系人”,并立即失败。
HasMany(x => x.Contacts).KeyColumn("supplierId").Inverse();