0

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

我正在使用 Fluent Nhibernate 自动映射器(和一些覆盖)来让这个结构工作。

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

我已经尝试过各种各样的事情。如果你能指出我正确的方向,那就太好了。:)

谢谢蒂姆

4

1 回答 1

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.Game它们GameTeam.Team

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

于 2012-12-12T13:58:46.703 回答