2

我有一个名为ProjectTaskBudgettype 的类IBudgetableIncomeStream,如下所示:

public class ProjectTaskBudget
{
    public virtual IBudgetableIncomeStream BudgetableIncomeStream { get; set; }
}

IBudgetableIncomeStream是一个接口,除其他外,它应用于一个名为的抽象类Deliverable

public abstract class Deliverable : IBudgetableIncomeStream
{
    // Some stuff
}

和一个名为的具体类RecurringIncomeLine

public class RecurringIncomeLine : IBudgetableIncomeStream
{
    // Some stuff
}

继承自的具体类DeliverableDeliverableItemDeliverableTime

的映射ProjectTaskBudget如下所示:

public class ProjectTaskBudgetMap : MappingBase<ProjectTaskBudget>
{
    public ProjectTaskBudgetMap()
    {
        ReferencesAny(n => n.BudgetableIncomeStream)
            .EntityTypeColumn("IncomeStreamType")
            .EntityIdentifierColumn("IncomeStream_id")
            .AddMetaValue<Deliverable>("Deliverable")
            .AddMetaValue<RecurringIncomeLine>("RecurringIncomeLine")
            .IdentityType<Guid>();
    }
}

这与 . 一样有效RecurringIncomeLine,所以我怀疑问题与抽象/接口映射有关。当我尝试ProjectTaskBudget使用Deliverable, 和刷新向数据库添加新的时,我收到此错误:

NHibernate.PropertyValueException: Error dehydrating property value for xxx.xxx.xxx.ProjectTaskBudget.BudgetableIncomeStream ---> System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at NHibernate.Type.MetaType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session)
   at NHibernate.Type.AnyType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
   --- End of inner exception stack trace ---
   at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
   at NHibernate.Action.EntityInsertAction.Execute()
   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
   at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
   at NHibernate.Engine.ActionQueue.ExecuteActions()
   at hiJump.Infrastructure.DataAccess.EventListeners.FixedDefaultFlushEventListener.PerformExecutions(IEventSource session) in c:\code\Git\Harmony\SharedCode\hiJump\hiJump.Infrastructure\DataAccess\EventListeners\FixedDefaultFlushEventListener.cs:line 35
   at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
   at NHibernate.Impl.SessionImpl.Flush()

我想知道是否将接口直接应用到DeliverableItemDeliverableTime会有所作为,并让映射包括

.AddMetaValue<DeliverableItem>("DeliverableItem")
.AddMetaValue<DeliverableTime>("DeliverableTime")

但这是一个相当大的重构,所以我想先检查一下,看看我是否遗漏了更明显的东西,或者这听起来像是无稽之谈。

4

1 回答 1

2

好的,我想通了。问题出在映射中,但不是我想的那样。

IBudgetableIncomeStream它试图添加的实际上是从 派生的东西,DeliverableItem所以映射 DeliverableItem是不够的,我必须在映射的每个子类中放入 DeliverableItemDeliverableTime,例如:

.AddMetaValue<DeliverableItemSubclass1>("DeliverableItemSubclass1")
.AddMetaValue<DeliverableItemSubclass2>("DeliverableItemSubclass2")
.AddMetaValue<DeliverableTimeSubclass1>("DeliverableTimeSubclass1")
于 2013-02-14T19:15:20.890 回答