NHibernate映射约束违规

时间:2011-07-28 20:45:32

标签: nhibernate nhibernate-mapping

我正在做一些愚蠢的事情,我只是没有看到它。

我只希望类Party有一组PartyNames,使用下面的映射,并且无法插入具有FK约束违规的PartyName。如有必要,将发布目标代码,但怀疑它是映射。

干杯,
Berryl

错误和测试代码

NHibernate: INSERT INTO Parties (Type, PartyId) VALUES ('PERSON', @p0);@p0 = 229376 [Type: Int32 (0)]
NHibernate: INSERT INTO PartyNames (PartyId, RequiredName, EverythingElse, ContextUsed, Salutation, EffectiveStart, EffectiveEnd, PartyNameId) 
    VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7);
    @p0 = 229376 [Type: Int32 (0)], 
    @p1 = 'Hesh' [Type: String (50)], 
    @p2 = 'Berryl;;;' [Type: String (4000)], 
    @p3 = 'Stack Overflow Profile' [Type: String (50)], 
    @p4 = 'Fellow Geek' [Type: String (20)], 
    @p5 = 7/28/2011 1:21:19 PM [Type: DateTime (0)], 
    @p6 = 12/31/9999 11:59:59 PM [Type: DateTime (0)], 
    @p7 = 262144 [Type: Int32 (0)]
Test 'Parties.Data.Impl.NHib.Tests.TestFixtures.SqlServerCommandExecutor.GenerateTestData' failed: NHibernate.Exceptions.GenericADOException : could not insert: [Parties.Domain.Names.PartyName#262144][SQL: INSERT INTO PartyNames (PartyId, RequiredName, EverythingElse, ContextUsed, Salutation, EffectiveStart, EffectiveEnd, PartyNameId) VALUES (?, ?, ?, ?, ?, ?, ?, ?)]
  ----> System.Data.SqlClient.SqlException : The INSERT statement conflicted with the FOREIGN KEY constraint "Party_PartyName_FK". The conflict occurred in database "PartyDomainDb", table "dbo.Parties", column 'PartyId'.

    [Test, Explicit]
    public void GenerateTestData()
    {
        NameSeeds.Initialize();

        var party = new Person();

        using (Scope(true))
        {
            _session.SaveOrUpdate(party);
            NameSeeds.DevName.Party = party;
            NameSeeds.LegalName.Party = party;
            Assert.That(party.Names.Count(), Is.EqualTo(2));
            _session.SaveOrUpdate(party);
        }
    }

映射(派对)

   <class name="Party" table="Parties">
    <id name="Id">
      <column name="PartyId" />
      <generator class="hilo" />
    </id>

    <discriminator column="Type" not-null="true" type="string" />

    <set access="field.camelcase-underscore" cascade="all-delete-orphan" inverse="true" name="Names">
      <key foreign-key="Party_PartyName_FK">
        <column name="PartyNameId" />
      </key>
      <one-to-many class="Parties.Domain.Names.PartyName, Parties.Domain" />
    </set>

    <subclass 
      name="Parties.Domain.People.Person, Parties.Domain" 
      discriminator-value="PERSON"
      >

    </subclass>

映射(PartyName)

<class name="PartyName" table="PartyNames">
<id name="Id">
  <column name="PartyNameId" />
  <generator class="hilo" />
</id>

<natural-id>
  <many-to-one access="field.camelcase-underscore" class="Parties.Domain.Party" foreign-key="Party_FK" name="Party" cascade="save-update">
    <column name="PartyId" index="PartyIndex" not-null="true" />
  </many-to-one>
  <property name="RequiredName" not-null="true" length="50"/>
</natural-id>

<property name="EverythingElse" />
<property name="ContextUsed" length="50"/>
<property name="Salutation" length="20"/>
<property name="EffectivePeriod" type="Smack.Core.Data.NHibernate.UserTypes.DateRangeUserType, Smack.Core.Data">
  <column name="EffectiveStart"/>
  <column name="EffectiveEnd"/>
</property>

1 个答案:

答案 0 :(得分:0)

我是对的,这是映射,我是对的,它是愚蠢的

我把外键上的列名混淆了!

        <set access="field.camelcase-underscore" cascade="all-delete-orphan" inverse="true" name="Names">
      <key foreign-key="Party_PartyName_FK">
        <column name="PartyNameId" />    ** WRONG, what I had
        <column name="PartyId" />        ** RIGHT, what I umm, meant
      </key>
      <one-to-many class="Parties.Domain.Names.PartyName, Parties.Domain" />
    </set>