流利的NHibernate复合复合Id

时间:2022-08-24 15:39:59

I've been trying to get the following to work for a few hours now. But I can't seem to find out what I'm doing wrong here.

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

I'm using Fluent Nhibernate automapper (and some overrides) to get this structure to work.

我正在使用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; }
}

The GameTeam relation is something I can manage. But the GameTeamRound link is going a bit to far for the moment. :) I even have a level deeper. But I don't want to make the question more complicated.

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

I'm using NHibernate to generate my database for me. So I'm starting from my model. To make this work I'm using some mapping overrides to make sure that these composite keys are working.

我正在使用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");
    }
}

I've tried al sort of things. If you could point me into the right direction, that would be great. :)

我尝试了很多东西。如果你能指出我正确的方向,那就太好了。 :)

Thanks Tim

1 个解决方案

#1


0  

Why the extra class for the composite key?
It should be working somehow like this:

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

Entities:

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

Mappings:

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

Please note: I am not sure if it even is possible to use this kind of nesting in a composite ID. If not, you would need to add properties for Game and Team and delegate the calls to them to GameTeam.Game and GameTeam.Team respectivly.

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

Additionally, please re-consider your design. Composite keys are discouraged for newly written applications. If they can be avoided, avoid them.

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

#1


0  

Why the extra class for the composite key?
It should be working somehow like this:

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

Entities:

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

Mappings:

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

Please note: I am not sure if it even is possible to use this kind of nesting in a composite ID. If not, you would need to add properties for Game and Team and delegate the calls to them to GameTeam.Game and GameTeam.Team respectivly.

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

Additionally, please re-consider your design. Composite keys are discouraged for newly written applications. If they can be avoided, avoid them.

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