NHibernate Mapping升级到NHibernate 3.1时中断

时间:2011-08-08 02:07:03

标签: .net fluent-nhibernate nhibernate-mapping

我刚刚升级到使用NHibernate 3.1的FluentNHibernate 1.2。升级后,我的一些旧映射无法正常工作。我一直很难搞清楚为什么,我想也许有人可以帮忙。

我有3个课程,练习,练习和练习钻。练习中有许多练习和练习可以在许多练习中。 PracticeDrill是连接它们的表,还包括一个Order。这是我的C#POCO:

public class PracticeDrill
{
    public virtual Practice Practice { get; set; }
    public virtual Drill Drill { get; set; }
    public virtual int Order { get; set; }
}

public class Practice
{
    public virtual Guid Id { get; set; }
    public virtual ICollection<PracticeDrill> Drills { get; set; }

    public Practice()
    {
        Drills = new List<PracticeDrill>();
    }
}

public class Drill
{
    public virtual Guid Id { get; set; }
}

这是我的映射文件的样子:

public class PracticeDrillMap : ClassMap<PracticeDrill>
{
    public PracticeDrillMap()
    {
        CompositeId()
            .KeyReference(x => x.Practice, "PracticeId")
            .KeyReference(x => x.Drill, "DrillId");
        Map(x => x.Order)
            .Column("[Order]")
            .Not.Nullable();
    }
}

public class PracticeMap : ClassMap<Practice>
{
    public PracticeMap()
    {
        Id(x => x.Id)
            .GeneratedBy.GuidComb();
        HasMany(x => x.Drills)
            .KeyColumn("PracticeId")
            .AsBag()
            .Inverse()
            .Cascade.AllDeleteOrphan();
    }
}

public class DrillMap : ClassMap<Drill>
{
    public DrillMap()
    {
        Id(x => x.Id)
            .GeneratedBy.GuidComb();
    }
}

之前允许我创建/删除引用Drills的实践。现在,当我尝试删除练习时,我得到以下异常:

{System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.    
at System.ThrowHelper.ThrowKeyNotFoundException()    
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)    
at NHibernate.Engine.StatefulPersistenceContext.RemoveEntity(EntityKey key) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\StatefulPersistenceContext.cs:line 444    
at NHibernate.Action.EntityDeleteAction.Execute() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Action\EntityDeleteAction.cs:line 87    
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:line 136    
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:line 126    
at NHibernate.Engine.ActionQueue.ExecuteActions() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:line 174    
at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs:line 241    
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\DefaultFlushEventListener.cs:line 19    
at NHibernate.Impl.SessionImpl.Flush() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 1472    
at NHibernate.Transaction.AdoTransaction.Commit() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Transaction\AdoTransaction.cs:line 187    
at GoldMedalSquared.Toolbox.Data.NHibernate.RepositoryBase`1.WrapInTransaction(ISession session, Action query) in C:\Work\primary\trunk\Toolbox\Data.NHibernate\RepositoryBase.cs:line 42}

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您只使用一个外键(ICollection<PracticeDrill> Drills)映射.KeyColumn("PracticeId"),但它实际上有一个复合键。因此,NH无法通过其ID PracticeDrill来细化。

整个映射有点奇怪。为什么使用引用类?如果真的有必要,为什么要使用复合键呢?