0
我正在做一些愚蠢的事情,我只是沒有看到它。NHibernate映射約束違規
我只想讓Party 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>
沒有NameSeeds和Scope的代碼很難回答。 –
@Diego Mijelshon。嗨迭戈,看到更新後的帖子。我還加入了維護雙向關係的代碼 - 我知道它爲SO問題提供了一些代碼。乾杯 – Berryl