实体框架核心两个对象作为主键 [英] Entity Framework Core Two Objects as Primary Key

查看:138
本文介绍了实体框架核心两个对象作为主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于管理朋友关系的模型。它看起来如下:

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; }
}    

用户的ID有多条记录,并且有多条记录相同的FriendID,因此不能将其中任何一个定义为键。我希望键是用户和朋友之间的组合,但是当我这样定义时:

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. I would like the key to be a composite between User and Friend but when I define it like this:

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

推荐答案

此处的基本思想是,您向EF添加的属性可以用于建立关系。正确,您正在尝试创建类型为 User 的关系,这会产生错误。要分配组合键,每个键都必须是与 Key 字段兼容的类型,而不是导航属性。所以我们添加 UserId FriendId 类型为 int , code> string 或 GUID 等,并根据这些属性创建关系。

The basic idea here is that your adding properties to the model that EF can use to make a relationship. Right you're trying to create a relationship of type User and that is creating an error. To assign a composite key each key needs to be a type compatible with a Key field, not a navigation property. So we add UserId and FriendId of type int, string or GUID etc. and create a relationship off those properties.

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; }
}

您现在可以跨 UserId定义组合键 FriendId 。这样的事情应该可以做:

You can now define a composite key across UserId and FriendId. 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; }
}

来源:中-如何:实体框架核心关系,复合键,外部键,数据注释,Code First和Fluent API

这篇关于实体框架核心两个对象作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆