如何使用NHibernate和SQL Server CE来加载子集合?

时间:2012-01-20 23:22:42

标签: nhibernate eager-loading

我有以下3个实体:

class Match
{
    IList<Possession> Possessions { get; set; }
    IList<Action> Actions { get; set; }
}

class Possession
{
    Match ParentMatch { get; set; }
    Action StartAction { get; set; }
    Action EndAction { get; set; }
}

class Action
{
    Match ParentMatch { get; set; }
    Possession ParentPossession  { get; set; }
}

和相应的映射:

mapper.Class< Possession >( cm =>
{
    cm.ManyToOne( p => p.Match, pm =>
                                    {
                                        pm.Access( Accessor.Field );
                                        pm.Column( "MatchId" );
                                    } );

    cm.ManyToOne( p => p.StartAction, pm =>
                                        {
                                            pm.Cascade( Cascade.Persist );
                                            pm.Access( Accessor.Field );
                                            pm.Column( "StartActionId" );
                                        } );

    cm.ManyToOne( p => p.EndAction, pm =>
                                        {
                                            pm.Cascade( Cascade.Persist );
                                            pm.Access( Accessor.Field );
                                            pm.Column( "EndActionId" );
                                            pm.NotNullable( false );
                                        } );
} );

mapper.Class< Action >( cm =>
{
    cm.ManyToOne( a => a.Match, pm => pm.Column( "MatchId" ) );

    cm.ManyToOne( a => a.Possession,
                  pm =>
                    {
                        pm.NotNullable( false );
                        pm.Column( "PossessionId" );
                    } );
} );

mapper.Class< Match >( cm =>
{
    cm.Bag( m => m.Actions,
            pm =>
                {
                    pm.Cascade( Cascade.All | Cascade.DeleteOrphans );
                    pm.Key( km => km.Column( "MatchId" ) );
                    pm.Inverse( true );
                    pm.OrderBy( a => a.ActionsOrder );
                },
            rel => rel.OneToMany() );

    cm.Bag( m => m.Periods,
            pm =>
                {
                    pm.Cascade( Cascade.All | Cascade.DeleteOrphans );
                    pm.Key( km => km.Column( "MatchId" ) );
                    pm.Inverse( true );
                    pm.OrderBy( p => p.PeriodsOrder );
                },
            rel => rel.OneToMany() );

    cm.Bag( m => m.Possessions,
            pm =>
                {
                    pm.Cascade( Cascade.All | Cascade.DeleteOrphans );
                    pm.Key( km => km.Column( "MatchId" ) );
                    pm.Inverse( true );
                    pm.OrderBy( p => p.PossessionsOrder );
                },
            rel => rel.OneToMany() );
} );

我有一个加载Match的查询,我希望从数据库中检索Possessions和Actions属性,并设置它们的ID。

听起来没什么大不了的,但是我不能成功,因为我的所有财产的Id都是0。

我试过这样的比赛:

return Session.Get< Match >( id );

奇怪的是,只有财产没有身份证。动作列表中填充了所有具有正确ID的实体。

我试图在映射中停用集合的懒惰,但它没有改变任何东西。也没有将CollectionFetchMode更改为Select。

我也试过使用NH的Future,但SQL Server CE不支持它们。

所以......我被困了

我该如何解决这个问题?

0 个答案:

没有答案