[英]How to create 2 string foreign keys pointing at the same unique string column?
I have an entity "Player" that has a "Guid" property of type string, with the unique constraint. 我有一个实体“ Player”,它具有字符串类型的“ Guid”属性,具有唯一性约束。
I have a "PlayerData" entity, that has the properties "PlayerGuid", "OpponentPlayerGuid" of type string. 我有一个“ PlayerData”实体,该实体具有字符串类型的“ PlayerGuid”,“ OpponentPlayerGuid”属性。
How do I use EF Core to make sure PlayerGuid and OpponentPlayerGuid properties accept values only found inside Player.Guid property? 如何使用EF Core确保PlayerGuid和OpponentPlayerGuid属性接受仅在Player.Guid属性中找到的值?
I've tried various things, from probably 5+ different answers on stackoverflow, none of which helped me work this out. 我已经尝试了各种方法,从关于stackoverflow的5个以上不同的答案中,没有一个能帮助我解决这个问题。
PlayerData entity PlayerData实体
public class PlayerData
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int AttackLevel { get; set; } = 0;
public int DefenseLevel { get; set; } = 0;
[InverseProperty("Guid")]
public string PlayerGuid { get; set; }
public Player Player { get; set; }
//[ForeignKey("OpponentPlayerGuid")]
//public string OpponentPlayerGuid { get; set; }
public int WinStreak { get; set; } = 0;
public double Armor { get; set; }
public double Health { get; set; }
public bool Acted { get; set; }
public ActionType LastAction { get; set; }
}
Player entity 玩家实体
public class Player
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("SessionId")]
public int? SessionId { get; set; }
public Session Session { get; set; }
[Required,MaxLength(36)]
public string Guid { get; set; }
public DateTime? BanTime { get; set; }
public List<PlayerData> PlayerDatas { get; set; }
}
You need to specify Alternate key on guid. 您需要在guid上指定Alternate键。 And then configure foreign keys to it.
然后为其配置外键。 Do it by using fluent API.
通过使用流畅的API来做到这一点。 Examples of usage here: https://docs.microsoft.com/en-US/ef/core/modeling/alternate-keys .
此处的用法示例: https : //docs.microsoft.com/zh-CN/ef/core/modeling/alternate-keys 。
In your DbContext class: 在您的DbContext类中:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PlayerData>()
.HasOne(e => e.Player)
.WithMany() // If its a one to one relationsship, specify 'WithOne()' here
.HasForeignKey(e => e.PlayerGuid)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<PlayerData>()
.HasOne(e => e.OpponentPlayer)
.WithMany()
.HasForeignKey(e => e.OpponentPlayerGuid)
.OnDelete(DeleteBehavior.Restrict);
}
On a side note: You should use the value type Guid
, provided by the BCL, instead of strings. 附带说明:您应该使用BCL提供的值类型
Guid
而不是字符串。 That way your ID's will always be a Guid. 这样,您的ID将永远是Guid。
If you don't mind me asking, is there any specific reason why you are not using Id to reference Player and OpponentPlayer? 如果您不介意我问,是否有任何特定原因导致您不使用Id来引用Player和OpponentPlayer? Then you can use foreign key to reference Player table.
然后,您可以使用外键引用Player表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.