【发布时间】:2016-05-01 06:35:04
【问题描述】:
我有一组通过FriendLinker 表链接在一起的玩家。
桌子将两个玩家联系在一起(在本例中,它的玩家->朋友)。我通过以下方式设置播放器:
public class Player
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key, Column(Order=0)]
public Guid PlayerId { get; set; }
public virtual ICollection<FriendLinker> Friends { get; set; }
[Required]
public string Password { get; set; } //Will be switched to byte[] for hash
[MaxLength(100)]
[Index(IsUnique = true)]
public string Username { get; set; }
}
链接器表是这样设置的:
public class FriendLinker
{
[Key]
public int FriendLinkerId { get; set; }
[Required]
public Player Player { get; set; }
[Required]
public Player Friend { get; set; }
}
但是,这会产生以下迁移:
CreateTable(
"dbo.FriendLinkers",
c => new
{
FriendLinkerId = c.Int(nullable: false, identity: true),
Player_PlayerId = c.Guid(),
Friend_PlayerId = c.Guid(nullable: false),
Player_PlayerId1 = c.Guid(nullable: false),
})
.PrimaryKey(t => t.FriendLinkerId)
.ForeignKey("dbo.Players", t => t.Player_PlayerId)
.ForeignKey("dbo.Players", t => t.Friend_PlayerId, cascadeDelete: false)
.ForeignKey("dbo.Players", t => t.Player_PlayerId1, cascadeDelete: false)
.Index(t => t.Player_PlayerId)
.Index(t => t.Friend_PlayerId)
.Index(t => t.Player_PlayerId1);
结果会创建一个额外的列Player_PlayerId1。当我执行player.Friends.add(..) 时,playerId 被插入到 PlayerId1 中。
我应该怎么做才能防止生成额外的列PlayerId1?
【问题讨论】:
标签: c# entity-framework code-first