Entity framework 特定实体框架代码第一个多到2模型映射

Entity framework 特定实体框架代码第一个多到2模型映射,entity-framework,foreign-keys,ef-code-first,Entity Framework,Foreign Keys,Ef Code First,我在这里有点不知所措 基本上我有这两种型号: public class Player { public int PlayerId { get; set; } public string Name { get; set; } public virtual ICollection<Game> Games { get; set; } } public class Game { public int GameId { get; set; } pu

我在这里有点不知所措

基本上我有这两种型号:

public class Player
{
    public int PlayerId { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Game> Games { get; set; }
}

public class Game
{
    public int GameId { get; set; }

    public virtual Player PlayerBlack { get; set; }
    public virtual Player PlayerWhite { get; set; }
}
公共类播放器
{
public int PlayerId{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection游戏{get;set;}
}
公开课游戏
{
public int GameId{get;set;}
公共虚拟播放器PlayerBlack{get;set;}
公共虚拟播放器播放器白色{get;set;}
}
现在数据库模式EF Code首先为我创建的,是不正确的,因为游戏表得到3个外键(Playerblack、PlayerWhite和Player),而不是2个

因此,我如何将这些模型联系在一起,以便EF了解玩家游戏是通过查看黑人或白人玩家发现的

基本上每次我打电话给我的玩家时,游戏EF都要查看PlayerBlack和PlayerWhite外键


甚至可能吗?

我认为这是不可能的。不能在关系的一侧有一个端点,在另一侧有两个端点

可能的解决办法:

  • Player
    类中使用两个集合:

    public virtual ICollection<Game> GamesAsBlackPlayer { get; set; }
    public virtual ICollection<Game> GamesAsWhitePlayer { get; set; }
    

    通过
    PlayerBlack
    中的
    Single
    方法和
    Game
    类中的
    PlayerWhite
    属性可以看到,您必须确保在业务逻辑中创建适当的
    playerGame
    实体,以便
    PlayersInGame
    集合始终有两个黑色或黑色元素分别为白色标志。

    我将这样处理问题:

    public abstract class Player
    {
        public int ID { get; set; }
    }
    
    public class WhitePlayer : Player
    {
    }
    
    public class BlackPlayer : Player
    {
    }
    
    public class Game
    {
        public int ID { get; set; }
    
        public virtual WhitePlayer WhitePlayer { get; set; }
        public virtual BlackPlayer BlackPlayer { get; set; }
    }
    
    public class GamePlayerContext : DbContext
    {
        public DbSet<Game> Games { get; set; }
        public DbSet<Player> Players { get; set; }
    }
    
    公共抽象类播放器
    {
    公共int ID{get;set;}
    }
    公共类白人玩家:玩家
    {
    }
    公共类黑玩家:玩家
    {
    }
    公开课游戏
    {
    公共int ID{get;set;}
    公共虚拟WhitePlayer WhitePlayer{get;set;}
    公共虚拟BlackPlayer BlackPlayer{get;set;}
    }
    公共类GamePlayerContext:DbContext
    {
    公共DbSet游戏{get;set;}
    公共DbSet播放器{get;set;}
    }
    
    BTW:这里也有类似的问题:当你正在将两个引用映射到一个集合时,我正在寻找将两个集合映射到一个引用的方法。谢谢你的回答。我接受了你的第一个想法,并获得了所有游戏:
    public IEnumerable games{get{return GamesAsBlackPlayer.Concat(GamesAsWhitePlayer.ToList();}}
    Ah,Concat!我甚至不知道这个方法存在。(我脑海中有一个列表是“AddRange”,但Concat似乎更一般。很好!)你的解决方案不正确,因为同一个玩家有时是黑人,有时是白人。对象无法更改其类型。
    public abstract class Player
    {
        public int ID { get; set; }
    }
    
    public class WhitePlayer : Player
    {
    }
    
    public class BlackPlayer : Player
    {
    }
    
    public class Game
    {
        public int ID { get; set; }
    
        public virtual WhitePlayer WhitePlayer { get; set; }
        public virtual BlackPlayer BlackPlayer { get; set; }
    }
    
    public class GamePlayerContext : DbContext
    {
        public DbSet<Game> Games { get; set; }
        public DbSet<Player> Players { get; set; }
    }