简体   繁体   English

实体框架核心两个对象作为主键

[英]Entity Framework Core Two Objects as Primary Key

I have a model which is used for managing friend relationships. 我有一个用于管理朋友关系的模型。 It looks as follows: 它看起来如下:

public class Relationship
{   
   [Required]
   public User User { get; set; }

   [Required]
   public User Friend { get; set; }

   [Required]
   public DateTimeOffset RelationshipInitializationDate { get; set; }
}    

Users will have multiple records for their ID and there will be multiple records with the same FriendID so defining either of these as a key is a no-go. 用户将拥有其ID的多个记录,并且将有多个具有相同FriendID的记录,因此将其中任何一个定义为密钥都是禁止的。 I would like the key to be a composite between User and Friend but when I define it like this: 我希望关键是User和Friend之间的复合,但是当我像这样定义它时:

modelBuilder.Entity<Relationship>().HasKey(r => new { r.User, r.Friend });

I get an error that states: 我收到一条错误消息:

The property 'Relationship.User' is of type 'User' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

How should I go about this to create the primary key that will link with a user and friend object. 我应该如何创建将与用户和朋友对象链接的主键。 I didn't have any issues with my other objects having typed properties and I don't have an issue if I add an arbitrary key to the Relationship model. 我的其他对象具有类型属性我没有任何问题,如果我向关系模型添加任意键,我没有问题。 Thanks in advance 提前致谢

The basic idea here is that your adding properties to the model that EF can use to make a relationship. 这里的基本思想是,您可以使用EF为模型添加属性来建立关系。 Right you're trying to create a relationship of type User and that is creating an error. 你正试图创建一个User类型的关系,那就是创建一个错误。 To assign a composite key each key needs to be a type compatible with a Key field, not a navigation property. 要分配复合键,每个键必须是与Key字段兼容的类型,而不是导航属性。 So we add UserId and FriendId of type int , string or GUID etc. and create a relationship off those properties. 因此,我们添加类型为intstringGUID等的UserIdFriendId ,并创建这些属性之间的关系。

public class Relationship
{
    public User Friend { get; set; }

    public User User { get; set; }

    public int FriendId { get; set; }

    public int UserId { get; set; }

    public DateTimeOffset RelationshipInitializationDate { get; set; }
}

public class User
{
    public int UserId { get; set; }
}

You can now define a composite key across UserId and FriendId . 您现在可以在UserIdFriendId之间定义组合键。 Something like this should do: 这样的事情应该做:

public class NorthwindContext : DbContext
{
     public NorthwindContext(DbContextOptions<NorthwindContext> options):base(options) { }

     public NorthwindContext() { }

     protected override void OnModelCreating(ModelBuilder builder)
     {
         builder.Entity<Relationship>().HasKey(table => new {
         table.FriendId, table.UserId
         });
     }
     public DbSet<Relationship> Relationships { get; set; }
     public DbSet<User> Users { get; set; }
}

Source: Medium - How To: Entity Framework Core relationships, composite keys, foreign keys, data annotations, Code First and Fluent API 来源:媒体 - 如何:实体框架核心关系,复合键,外键,数据注释,Code First和Fluent API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM