简体   繁体   English

NHibernate +多个外键到同一列

[英]NHibernate + Multiple foreign keys to same column

I'm trying to map my Team and Match table by using following mappings: 我正在尝试通过使用以下映射来映射我的TeamMatch表:

// Team.cs // Team.cs

public class Team
{
    public virtual int ID { get; private set; }
    public virtual string TeamName { get; set; }
    public virtual Cup Cup { get; set; }
    public virtual IList<Match> Matches { get; set; }

    public Team()
    {
        Matches = new List<Match>();
    }
}

public class TeamMap : ClassMap<Team>
{
    public TeamMap()
    {
        Id(x => x.ID);
        Map(x => x.TeamName).Not.Nullable();
        References(x => x.Cup, "CupID");

        HasMany(x => x.Matches)
            .Key(x => x.Columns.Add("Team1ID", "Team2ID"))
            .Inverse().Cascade.AllDeleteOrphan();

        Table("Teams");
    }
}

// Match.cs // Match.cs

public class Match
{
    public virtual int ID { get; private set; }
    public virtual Team Team1 { get; set; }
    public virtual Team Team2 { get; set; }
    public virtual int WinnerID { get; set; }
    public virtual Cup Cup { get; set; }
}

public class MatchMap : ClassMap<Match>
{
    public MatchMap()
    {
        Id(x => x.ID);
        Map(x => x.WinnerID);
        References(x => x.Team1, "Team1ID");
        References(x => x.Team2, "Team2ID");
        References(x => x.Cup, "CupID");
        Table("Matches");
    }
}

However, it throws an exception that says: 但是,它引发了一个异常,指出:

Foreign key (FKEFFCA4CA45169AED:Matches [Team1ID, Team2ID])) must have same number of columns as the referenced primary key (Teams [ID]) 外键(FKEFFCA4CA45169AED:Matches [Team1ID,Team2ID])必须与引用的主键(Teams [ID])具有相同的列数

Any suggestions? 有什么建议么?

UPDATE: 更新:

I was able to solve it by mixing something together based on the comment wrote by @Yads. 我能够通过基于@Yads发表的评论将某些东西混合在一起来解决它。

My code: 我的代码:

// Team.cs // Team.cs

public class Team
{
    public virtual int ID { get; private set; }
    public virtual string TeamName { get; set; }
    public virtual Cup Cup { get; set; }
    public virtual IList<Match> HomeMatches { get; set; }
    public virtual IList<Match> AwayMatches { get; set; }
    public virtual IList<Match> Matches { get { return HomeMatches.Concat(AwayMatches).ToList(); }}

    public Team()
    {
        HomeMatches = new List<Match>();
        AwayMatches = new List<Match>();
    }
}

public class TeamMap : ClassMap<Team>
{
    public TeamMap()
    {
        Id(x => x.ID);
        Map(x => x.TeamName).Not.Nullable();
        References(x => x.Cup, "CupID");

        HasMany(x => x.HomeMatches).KeyColumn("HomeTeamID")
            .Inverse().Cascade.AllDeleteOrphan();

        HasMany(x => x.AwayMatches).KeyColumn("AwayTeamID")
            .Inverse().Cascade.AllDeleteOrphan();
        Table("Teams");
    }
}

// Match.cs // Match.cs

public class Match
{
    public virtual int ID { get; private set; }
    public virtual Team HomeTeam { get; set; }
    public virtual Team AwayTeam { get; set; }
    public virtual int WinnerID { get; set; }
    public virtual Cup Cup { get; set; }
}

public class MatchMap : ClassMap<Match>
{
    public MatchMap()
    {
        Id(x => x.ID);
        Map(x => x.WinnerID);
        References(x => x.HomeTeam, "HomeTeamID");
        References(x => x.AwayTeam, "AwayTeamID");
        References(x => x.Cup, "CupID");
        Table("Matches");
    }
}

However, I'm not aware of what drawbacks this approach have... The .Concat() seems a bit nasty to me... 但是,我不知道这种方法有什么缺点。.Concat()对我来说似乎有点讨厌。

I don't know why, but I feel like I've answered this exact question before... 我不知道为什么,但是我觉得我之前已经回答了这个确切的问题...

The error you're seeing is occurring because you're creating an association based upon a composite inverse key, which won't be able to be mapped to a single foreign key. 您正在看到的错误正在发生,因为您正在基于复合反向键创建关联,该反向键将无法映射到单个外键。

One solution would be to try to shoehorn your "ideal" relational model into your object model, or, alternatively you could enrich your model. 一种解决方案是尝试将您的“理想”关系模型引入对象模型,或者,也可以丰富您的模型。 I suggest having a rich model to express a team playing in a match, you could call it Participation . 我建议您使用丰富的模型来表示比赛中的队伍,您可以将其称为“ Participation This would give you a many to many relationship between teams and matches. 这将为您提供团队与比赛之间的多对多关系。

The downside to this is you would need to express the cardinality of the relationship as a business rule (so you can't have three teams playing in the same match). 不利的一面是您需要将关系的基数表示为业务规则(因此,您不能让三支球队参加同一场比赛)。

The advantage is you could have a flag on the participation saying who the winner was, so that you don't need to have a WinnerId field on your object model. 这样做的好处是您可以在参与人员上标记一个胜利者是谁,这样您就不需要在对象模型上有一个WinnerId字段。

You'd end up with code that would be used like this: 您最终会得到如下使用的代码:

var match = new Match();
match.AddTeam(team1);
match.AddTeam(team2);

var winner = match.Participants.FindWinner();
winner = match.Winner; // alias for the above

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF:多列外键 - EF: Multiple column foreign keys NHibernate配置,外键 - NHibernate configuration, foreign keys NHibernate两列同时作为复合键和外键 - NHibernate two columns as composite key AND foreign keys at the same time NHibernate-通过不同的键对同一张表进行多个JOIN - NHibernate - multiple JOIN to the same table by different keys NHibernate-如何将具有多个外键的表映射到其对应的类? - NHibernate - How to map a table with multiple foreign keys to its corresponding classes? 具有多个相同外键的实体框架模型 - Entity Framework Model With Multiple Same Foreign Keys nHibernate通过SQLite支持外键 - nHibernate Support for Foreign Keys with SQLite 如果table1包含fk到table2的同一列的多个字段,如何从一个表创建多个外键到另一个表 - How to create multiple foreign keys from one table to another if table1 contains multiple fields with fk to the same column of table2 外键从同一张表中接收两个主键-Fluent Nhibernate - Foreign key receiving two primary keys from the same table - Fluent Nhibernate 如何创建两个指向同一唯一字符串列的字符串外键? - How to create 2 string foreign keys pointing at the same unique string column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM