Nhibernate 不理解这个错误

Nhibernate 不理解这个错误,nhibernate,nhibernate-mapping,Nhibernate,Nhibernate Mapping,鉴于下面的代码和映射,为什么会出现此错误 测试失败导致的错误 [测试] 公共虚空级联\u可以\u坚持\u英雄\u没有\u史诗() { _史诗=新史诗(“伊利亚”); var hero=新格里赫罗(“ted”); 英雄史诗(_epic); _session.SaveOrUpdate(_epic); _session.Flush(); _会议.驱逐(_epic); Assert.That(_epic.ispersist()); Assert.That(hero.ispersist()); var f

鉴于下面的代码和映射,为什么会出现此错误

测试失败导致的错误
[测试]
公共虚空级联\u可以\u坚持\u英雄\u没有\u史诗()
{
_史诗=新史诗(“伊利亚”);
var hero=新格里赫罗(“ted”);
英雄史诗(_epic);
_session.SaveOrUpdate(_epic);
_session.Flush();
_会议.驱逐(_epic);
Assert.That(_epic.ispersist());
Assert.That(hero.ispersist());
var found=\u session.Get(hero.Id);
找到。看();
断言。那(被发现,是。平等(英雄));
}
NHibernate:从hibernate\u unique\u键中选择next\u hi
NHibernate:更新hibernate_unique_key set next_hi=@p0,其中next_hi=@p1@p0=6[类型:Int32(0)],@p1=5[类型:Int32(0)]
NHibernate:从hibernate\u unique\u键中选择next\u hi
NHibernate:更新hibernate_unique_key set next_hi=@p0,其中next_hi=@p1@p0=7[类型:Int32(0)],@p1=6[类型:Int32(0)]
NHibernate:
插入Epic(EpicName,EpicId)
数值(@p0,@p1);
@p0='ILIAD'[类型:字符串(0)],0,
@p1=163840[类型:Int32(0)]
NHibernate:
插入GreekHero(HeroName、EpicId、GreekHeroId)
数值(@p0、@p1、@p2);
@p0='ted'[类型:字符串(0)],
@p1=163840[类型:Int32(0)],
@p2=196608[类型:Int32(0)]
测试“Core.Data.Tests.NHibernate.Bootstraps.MappingTests.CollectionMappingConstraintTests.Cascade\u可以\u持久化\u Hero\u而不使用\u Epic”失败:
NHibernate.Exceptions.GenericADOException:无法插入:
[Core.TestingSupport.GreekGods.Domain.GreekHero#196608][SQL:插入到GreekHero(HeroName,EpicId,GreekHeroId)值(?,,?)]
---->System.Data.SqlServerCe.SqlCeException:
无法插入外键值,因为相应的主键值不存在。[外键约束名称=FK25126F517FE25F6]
对象模型(父对象)
公共类史诗:实体
{
...
[UseDimplicity]//NHib使用此
公共史诗(){
_greekHeroes=新HashedSet();
}
/// 
///订单项的备份存储。从db加载时,NHib将设置此项。
/// 
私有只读Iesi.Collections.Generic.ISet\u greekHeroes;
/// 
///公众消费的希腊英雄的计数。
/// 
公共虚拟IEnumerable GreekHeroes{get{return\u GreekHeroes;}}
公共虚拟bool AddItem(GreekHero newItem)
{
如果(!ReferenceEquals(newItem,null)&&&U greekHeroes.Add(newItem))
{
newItem.SetEpic(这个);
返回true;
}
返回false;
}
公共虚拟bool removietem(GreekHero itemToRemove)
{
如果(!ReferenceEquals(itemToRemove,null)&&&U greekHeroes.Remove(itemToRemove))
{
itemToRemove.SetEpic(null);
返回true;
}
返回false;
}
}
对象模型(子对象)
公共类GreekHero:实体
{
...
#端区
#地域史诗(希腊英雄可能与史诗有关)
/// 
///与这位希腊英雄有关的史诗。
/// 
公共虚拟Epic Epic{get;protected set;}
/// 
///这将在对象模型中强制此对象之间的引用完整性
///还有。
/// 
///与这位英雄有关的新史诗。
///及
公共虚拟空间集Epic(Epic Epic)
{
var-prevEpic=Epic;
如果(epic==prevEpic)返回;
史诗=史诗;
如果(!ReferenceEquals(prevEpic,null))
prevEpic.RemoveItem(本);
如果(!ReferenceEquals(epic,null))
epic.AddItem(本);
}
#端区
}
HBM映射(父级)


HBM映射(子)



中的元素表示子表中存储父表键的列。在你的例子中,它应该是“EpicId”,因为这是你在中所说的。

我知道它是愚蠢的;只是不知道有那么愚蠢。谢谢
    [Test]
    public void Cascade_Can_Persist_Hero_Without_Epic()
    {
        _epic = new Epic("illiad");
        var hero = new GreekHero("ted");
        hero.SetEpic(_epic);
        _session.SaveOrUpdate(_epic);
        _session.Flush();
        _session.Evict(_epic);
        Assert.That(_epic.IsPersistent());
        Assert.That(hero.IsPersistent());

        var found = _session.Get<GreekHero>(hero.Id);
        found.Look();

        Assert.That(found, Is.EqualTo(hero));
    }

NHibernate: select next_hi from hibernate_unique_key
NHibernate: update hibernate_unique_key set next_hi = @p0 where next_hi = @p1;@p0 = 6 [Type: Int32 (0)], @p1 = 5 [Type: Int32 (0)]
NHibernate: select next_hi from hibernate_unique_key
NHibernate: update hibernate_unique_key set next_hi = @p0 where next_hi = @p1;@p0 = 7 [Type: Int32 (0)], @p1 = 6 [Type: Int32 (0)]
NHibernate: 
    INSERT INTO Epic (EpicName, EpicId) 
    VALUES (@p0, @p1);
        @p0 = 'illiad' [Type: String (0)], 
        @p1 = 163840 [Type: Int32 (0)]
NHibernate: 
    INSERT INTO GreekHero (HeroName, EpicId, GreekHeroId) 
    VALUES (@p0, @p1, @p2);
        @p0 = 'ted' [Type: String (0)], 
        @p1 = 163840 [Type: Int32 (0)], 
        @p2 = 196608 [Type: Int32 (0)]
Test 'Core.Data.Tests.NHibernate.Bootstraps.MappingTests.CollectionMappingConstraintTests.Cascade_Can_Persist_Hero_Without_Epic' failed:
    NHibernate.Exceptions.GenericADOException : could not insert: 
    [Core.TestingSupport.GreekGods.Domain.GreekHero#196608][SQL: INSERT INTO GreekHero (HeroName, EpicId, GreekHeroId) VALUES (?, ?, ?)]
  ----> System.Data.SqlServerCe.SqlCeException : 
  A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK253126F517FE25F6 ]
public class Epic : Entity
{
    ...

    [UsedImplicitly] // NHib uses this
    public Epic() {
        _greekHeroes = new HashedSet<GreekHero>();
    }

    /// <summary>
    /// The backing store for order items. NHib will set this when loaded from the db.
    /// </summary>
    private readonly Iesi.Collections.Generic.ISet<GreekHero> _greekHeroes;

    /// <summary>
    /// An enumeration of greek heros for public consumption.
    /// </summary>
    public virtual IEnumerable<GreekHero> GreekHeroes { get { return _greekHeroes; } }

    public virtual bool AddItem(GreekHero newItem)
    {
        if (!ReferenceEquals(newItem, null) && _greekHeroes.Add(newItem))
        {
            newItem.SetEpic(this);
            return true;
        }
        return false;
    }

    public virtual bool RemoveItem(GreekHero itemToRemove)
    {
        if (!ReferenceEquals(itemToRemove, null) && _greekHeroes.Remove(itemToRemove))
        {
            itemToRemove.SetEpic(null);
            return true;
        }
        return false;
    }
}
public class GreekHero : Entity
{
    ...

    #endregion

    #region Epic (a Greek Hero may be associated with an Epic)

    /// <summary>
    /// The Epic associated with this Greek Hero.
    /// </summary>
    public virtual Epic Epic { get; protected set; }

    /// <summary>
    /// This enforces referential integrity in the object model between this <see cref="GreekHero"/>
    /// and the <see cref="Domain.Epic.GreekHeroes"/>.
    /// </summary>
    /// <param name="epic">The new epic associated with this hero.</param>
    /// <remarks><seealso cref="Domain.Epic.AddItem"/> and <seealso cref="Domain.Epic.RemoveItem"/></remarks>
    public virtual void SetEpic(Epic epic)
    {
        var prevEpic = Epic;
        if (epic == prevEpic) return;

        Epic = epic;

        if (!ReferenceEquals(prevEpic, null))
            prevEpic.RemoveItem(this);

        if (!ReferenceEquals(epic, null))
            epic.AddItem(this);
    }

    #endregion
}
<id name="Id">
  <column name="EpicId" />
  <generator class="hilo" />
</id>

<natural-id>
  <property name="EpicName" length="30" />
</natural-id>

<set name="GreekHeroes"
     cascade="all-delete-orphan"
     inverse="true"
     access="field.camelcase-underscore">
  <key column="GreekHeroId" />
  <one-to-many class="GreekHero"/>
</set>
<id name="Id">
  <column name="GreekHeroId" />
  <generator class="hilo" />
</id>

<natural-id>
  <property name="HeroName" length="30" />
</natural-id>

<many-to-one name="Epic" column="EpicId" />