NHibernate的映射升级到3.1 NHibernate的时候打破升级到、时候、NHibernate

2023-09-03 07:14:34 作者:唯有、努力

我最近刚刚升级到FluentNHibernate 1.2,它使用的NHibernate 3.1。升级我的一些旧的映射后不工作。我一直有一个很难搞清楚为什么,我想也许有人在这里可以帮助。

I just recently upgraded to FluentNHibernate 1.2 which uses NHibernate 3.1. After upgrading some of my old mappings are not working. I've been having a hard time figuring out why, I thought maybe someone here could help.

我有3个班,实践,钻,和PracticeDrill。的实践有许多钻孔机和钻也可以在很多实践。 PracticeDrill的是,加入他们,其中还包括订单表格。下面是我的C#波苏斯:

I have 3 classes, Practice, Drill, and PracticeDrill. A Practice has many Drills and a Drill can be in many Practices. PracticeDrill is the table that joins them and also includes an Order. Here are my C# POCOs:

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; }
}

下面是我的映射文件如下:

Here is what my mapping files look like:

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();
    }
}

这previously让我创建引用钻/删除实践。现在,当我尝试删除的做法,我得到以下异常:

This previously allowed me to Create/Delete Practices that referenced Drills. Now when I try to Delete a practice I get the following exception:


{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}

有什么建议?

推荐答案

您映射的ICollection&LT; PracticeDrill&GT;钻只有一个外键( .KeyColumn(PracticeId)),但它实际上有一个复合键。因此NH无法通过其ID罚款的 PracticeDrill

You mapped ICollection<PracticeDrill> Drills with only one foreign key (.KeyColumn("PracticeId")), but it actually has a composite key. Therefore NH can't fine the PracticeDrill by its id.

整个映射是有点怪。为什么使用引用类?如果确有必要,为什么使用组合键?

The whole mapping is a bit strange. Why using a reference class? If it is really necessary, why using a composite key?