特定实体框架代码首先是多到2个模型映射 [英] Specific Entity Framework Code First Many to 2 Model Mapping
问题描述
基本上我有这两个模型:
public class Player
{
public int PlayerId {get;组; }
public string Name {get;组; }
public virtual ICollection< Game>游戏{get;组; }
}
public class Game
{
public int GameId {get;组; }
public virtual Player PlayerBlack {get;组; }
public virtual Player PlayerWhite {get;组; }
}
现在,数据库架构,EF代码首先为我创建,是不正确的因为游戏表中有3个外键(Playerblack,PlayerWhite和Player),而不是2。
那么我怎么能把这些模型结合在一起,这样EF就可以看到玩家游戏通过查看黑色或白色播放器可以找到。
基本上每次调用myPlayer.Games 时,EF必须查看PlayerBlack和PlayerWhite外号。
甚至有可能吗?
我相信这是不可能的。您不能与一方的单个端点和该关系另一端的两个端点相关联。
可能的解决方法:
-
在
Player
中使用两个集合:public virtual ICollection< Game> GamesAsBlackPlayer {get;组; }
public virtual ICollection< Game> GamesAsWhitePlayer {get;组; }
根据您的上下文,您可以将这些集合合并到一个只读集合
游戏
,它没有映射到数据库。
(编辑:现在是垃圾,现在删除。)
Edit2:另一个工作可能是通过一个额外的类来扩展你的类模型 PlayerInGame
:
public class Player
{
public int PlayerId {get;组; }
public string Name {get;组; }
public virtual ICollection< PlayerInGame> PlayerInGames {get;组; }
//可选帮助属性
[NotMapped]
public IEnumerable< Game>游戏
{
get
{
返回PlayerInGames.Select(g => g.Game);
}
}
}
public class Game
{
public int GameId {get;组; }
public virtual ICollection< PlayerInGame>玩家游戏{get;组; }
[NotMapped]
public Player PlayerBlack
{
get
{
return PlayersInGame.Single(p => p.WhiteOrBlack ==B)。
}
}
[NotMapped]
public Player PlayerWhite
{
get
{
return PlayersInGame。单(p => p.WhiteOrBlack ==W)。
}
}
}
public class PlayerInGame
{
public int PlayerInGameId {get;组; }
public virtual Game Game {get;组; }
public virtual Player Player {get;组; }
public string WhiteOrBlack {get;组; }
}
正如你可以看到的 Single
GameBlack
和 PlayerWhite
游戏中的属性
class你必须确保在您的业务逻辑中创建正确的 PlayerInGame
实体,以便您的 PlayersInGame
集合总是有两个元素,分别是黑色或白色标记。
I'm a bit of a loss here.
Basically I have these two Models:
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; }
}
Now the Database Schema, EF Code First creates for me, is not correct because the Game table gets 3 Foreign Keys (Playerblack, PlayerWhite and Player) instead of 2.
So how can I tie these Models together so that EF understands that the Players Games are found by either looking at the Black or White Player.
Basically each time I call myPlayer.Games EF has to look into PlayerBlack AND PlayerWhite Foreign Keys.
Is it even possible?
I believe it's not possible. You cannot have an association with a single endpoint on one side and two endpoints on the other side of the relation.
Possible workarounds:
Use two collections in the
Player
class:public virtual ICollection<Game> GamesAsBlackPlayer { get; set; } public virtual ICollection<Game> GamesAsWhitePlayer { get; set; }
Depending on your context you could perhaps merge these collections together to a readonly collection
Games
which is not mapped to the database.
(Edit: Proposed second workaround was crap, deleted now.)
Edit2: Another workarund could be to extend you class model by an additional class PlayerInGame
:
public class Player
{
public int PlayerId { get; set; }
public string Name { get; set; }
public virtual ICollection<PlayerInGame> PlayerInGames { get; set; }
// optional helper property
[NotMapped]
public IEnumerable<Game> Games
{
get
{
return PlayerInGames.Select(g => g.Game);
}
}
}
public class Game
{
public int GameId { get; set; }
public virtual ICollection<PlayerInGame> PlayersInGame { get; set; }
[NotMapped]
public Player PlayerBlack
{
get
{
return PlayersInGame.Single(p => p.WhiteOrBlack == "B").Player;
}
}
[NotMapped]
public Player PlayerWhite
{
get
{
return PlayersInGame.Single(p => p.WhiteOrBlack == "W").Player;
}
}
}
public class PlayerInGame
{
public int PlayerInGameId { get; set; }
public virtual Game Game { get; set; }
public virtual Player Player { get; set; }
public string WhiteOrBlack { get; set; }
}
As you can see by the Single
method in the PlayerBlack
and PlayerWhite
properties in the Game
class you have to make sure in your business logic to create the proper PlayerInGame
entities so that your PlayersInGame
collection always has two elements with Black or White flag respectively.
这篇关于特定实体框架代码首先是多到2个模型映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!