流利的NHibernate复合复合Id

时间:2012-12-12 13:47:58

标签: c# fluent-nhibernate asp.net-mvc-4

我一直试图让以下几个小时工作。但我似乎无法找出我在这里做错了什么。

我正在使用Fluent Nhibernate automapper(以及一些覆盖)来使这个结构起作用。

public class Game:IKeyed<Guid>
{
    public virtual Guid Id { get; set; }
    //properties
}

public class Team : IKeyed<Guid>
{
    public virtual Guid Id { get; set; }
    //Other properties

    public virtual IList<GameTeam> GameTeams { get; set; }
}

public class GameTeam:IKeyed<GameTeamId>
{
    public virtual GameTeamId Id { get; set; }
    public virtual int CurrentRound { get; set; }
    public virtual IList<GameTeamRound> Rounds { get; set; }
}

public class GameTeamId
{
    public virtual Game Game { get; set; }
    public virtual Team Team { get; set; }

    //equals stuff
}

public class GameTeamRound : IKeyed<GameTeamRoundId>
{
    public virtual GameTeamRoundId Id { get; set; }
    //Properties

    public virtual IList<TeamRoundDecision> Decisions { get; set; }
}

public class GameTeamRoundId
{
    public virtual GameTeam GameTeam { get; set; }
    public virtual int RoundNumber { get; set; }
}

GameTeam关系是我可以管理的。但GameTeamRound链接目前有点远。 :) 我甚至更深层次了。但我不想让问题更复杂。

我正在使用NHibernate为我生成数据库。所以我从我的模型开始。 为了完成这项工作,我正在使用一些映射覆盖来确保这些组合键正常工作。

public class GameTeamOverride : IAutoMappingOverride<GameTeam>
{
    public void Override(AutoMapping<GameTeam> mapping)
    {
        //mapping.IgnoreProperty(gt => gt.Id);
        mapping.CompositeId(gt => gt.Id)
               .KeyProperty(id => id.Game.Id, "GameId")
               .KeyProperty(id => id.Team.Id, "TeamId");
    }
}

public class GameTeamRoundOverride : IAutoMappingOverride<GameTeamRound>
{
    public void Override(AutoMapping<GameTeamRound> mapping)
    {
        //mapping.IgnoreProperty(gtr => gtr.Id);
        mapping.CompositeId(gtr => gtr.Id)
               .KeyProperty(id => id.GameTeam.Id.Game.Id, "GameId")
               .KeyProperty(id => id.GameTeam.Id.Team.Id, "TeamId")
               .KeyProperty(id => id.RoundNumber, "RoundId");
    }
}

我尝试过各种各样的事情。如果你能指出我正确的方向,这将是伟大的。 :)

由于 添

1 个答案:

答案 0 :(得分:0)

为什么复合键的额外类?
它应该以某种方式工作:

实体:

public class Game:IKeyed<Guid>
{
    public virtual Guid Id { get; set; }
    //properties
}

public class Team : IKeyed<Guid>
{
    public virtual Guid Id { get; set; }
    //Other properties

    public virtual IList<GameTeam> GameTeams { get; set; }
}

public class GameTeam:IKeyed<GameTeamId>
{
    public virtual Game Game { get; set; }
    public virtual Team Team { get; set; }
    public virtual int CurrentRound { get; set; }
    public virtual IList<GameTeamRound> Rounds { get; set; }
}

public class GameTeamRound : IKeyed<GameTeamRoundId>
{
    public virtual GameTeam GameTeam { get; set; }
    public virtual int RoundNumber { get; set; }
    //Properties

    public virtual IList<TeamRoundDecision> Decisions { get; set; }
}

映射:

public class GameTeamOverride : IAutoMappingOverride<GameTeam>
{
    public void Override(AutoMapping<GameTeam> mapping)
    {
        mapping.CompositeId()
               .KeyReference(id => id.Game, "GameId")
               .KeyReference(id => id.Team, "TeamId");
    }
}

public class GameTeamRoundOverride : IAutoMappingOverride<GameTeamRound>
{
    public void Override(AutoMapping<GameTeamRound> mapping)
    {
        mapping.CompositeId()
               .KeyReference(id => id.GameTeam.Game, "GameId")
               .KeyReference(id => id.GameTeam.Team, "TeamId")
               .KeyProperty(id => id.RoundNumber, "RoundId");
    }
}

请注意:我不确定是否可以在复合ID中使用这种嵌套。如果没有,您需要为Game和Team添加属性,并将对它们的调用分别委托给GameTeam.GameGameTeam.Team

此外,请重新考虑您的设计。对于新编写的应用程序,不鼓励使用复合键。如果可以避免,请避免使用它们。