简体   繁体   English

如何创建两个指向同一唯一字符串列的字符串外键?

[英]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.

相关问题 如何在一个表中创建两个指向某个表中同一列的外键? - How to Create two Foreign Keys in one Table pointing to the same Column in some table? 如何创建一个表有两个外键使用实体框架代码首先指向同一个表 - How to create a table with two foreign keys pointing to the same table using Entity Framework Code First 指向实体框架中相同表的多个外键是无法区分的 - Multiple foreign keys pointing to same table in Entity Framework are indistinguisible 实体框架-表需要两个指向同一表的外键 - Entity Framework - table needs two foreign keys pointing at same table 如何将字符串键映射到唯一的整数ID? - How to map string keys to unique integer IDs? Entity Framework 6 map 外键可以来自分隔字符串列吗? - Can Entity Framework 6 map foreign keys from a delimited string column? 如何将两个外键设置为同一父列(实体框架) - How to setup two foreign keys to the same parent column (Entity Framework) 如果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 NHibernate +多个外键到同一列 - NHibernate + Multiple foreign keys to same column 如何为数据库中的列生成唯一字符串? - How to generate a unique string for a column in a database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM