简体   繁体   English

使用ASP.NET核心在流畅的API中创建包含外键的复合主键

[英]Creating composite primary key consisting off foreign keys in fluent API with ASP.NET core

The question here is how can i generate a composite primary key consisting of two foreign keys? 这里的问题是如何生成由两个外键组成的复合主键?

I have tried: 我试过了:

public class ActiveQuestions_Questions 
{
    [Column(Order = 0), Key, ForeignKey("ActiveQuestion")]
    public string ActiveQuestionId {get; set;}

    [Column(Order = 1), Key, ForeignKey("Question")]
    public string QuestionId {get; set; }
}

Which gives me: Entity type 'ActiveQuestions_Questions' has composite primary key defined with data annotations. 这给了我:实体类型'ActiveQuestions_Questions'具有使用数据注释定义的复合主键。 To set composite primary key, use fluent API. 要设置复合主键,请使用fluent API。

Then i tried using the fluent api in the model builder without the annotations. 然后我尝试在没有注释的模型构建器中使用流畅的api。

builder.Entity<ActiveQuestions_Questions>(
            build => 
            {
                build.HasKey(t => new {t.ActiveQuestionId, t.QuestionId}); 
                build.HasOne(t => t.QuestionId).WithOne().HasForeignKey<Question>(qe => qe.QuestionId);
                build.HasOne(t => t.ActiveQuestionId).WithOne().HasForeignKey<ActiveQuestion>(qe => qe.ActivateQuestionId); 
            }
        );    

Which gave me : The navigation property 'QuestionId' cannot be added to the entity type 'ActiveQuestions_Questions' because a property with the same name already exists on entity type 'ActiveQuestions_Questions'. 这给了我:导航属性'QuestionId'无法添加到实体类型'ActiveQuestions_Questions',因为实体类型'ActiveQuestions_Questions'上已经存在具有相同名称的属性。

Anyone that can point me in the right direction? 谁能指出我正确的方向?

The Questions class 问题类

public class Question 
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public string QuestionId {get; set;}

    [Required]
    public string Text {get; set;}
}

The ActiveQuestion class: ActiveQuestion类:

public class ActiveQuestion 
{
    private DateTime _lastUpdated = DateTime.Now; 

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public string ActivateQuestionId {get; set;}

    public DateTime LastUpdated 
    {
        get 
        {
            return this._lastUpdated; 
        }
        set 
        {
            this._lastUpdated = value;
        }
    }
}

In EF Core, defining composite PK using KeyAttribute is not supported, therefore you must use fluent API to configure composite PK. 在EF Core中,不支持使用KeyAttribute定义复合PK,因此必须使用流畅的API来配置复合PK。

build.HasKey(t => new {t.ActiveQuestionId, t.QuestionId}); 

Above syntax you used in your code is correct way to define composite PK. 您在代码中使用的上述语法是定义复合PK的正确方法。 For more information see Keys in docs. 有关更多信息,请参阅文档中的

The reason your code fails is the incorrect configuration of relationship. 您的代码失败的原因是关系的错误配置。 HasOne / WithOne API are supposed to be used with navigation properties (properties which targets other entity types). HasOne / WithOne API应该与导航属性(定位其他实体类型的属性)一起使用。 In your configuration you are passing a primitive property in HasOne call. 在您的配置中,您将在HasOne调用中传递原始属性。 Since there is already property with same name added in the model (through conventions & HasKey call), it throws above exception. 由于模型中已经添加了具有相同名称的属性(通过约定和HasKey调用),因此它会抛出异常。 Even if they hadn't been added, there would be different exception. 即使没有添加它们,也会有不同的例外。 Here is the link to docs on how to define relationships using fluent API. 以下是有关如何使用流畅API定义关系的文档链接。

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

相关问题 ASP .NET Core MVC,Entity Framework,Identity——由外键组成的复合键 - ASP .NET Core MVC, Entity Framework, Identity - composite key consisting of foreign keys ASP.NET 添加迁移&#39;复合主键错误&#39; 如何使用 fluent API - ASP.NET add migration 'composite primary key error' how to use fluent API ASP.NET EF CORE:由两个外键组成的复合键无法正确评估 - ASP.NET EF CORE: Composite key composed of two foreign keys not evaluating properly Entity Framework Core - 复合键和外键作为主键 - Entity Framework Core - Composite and Foreign Keys as Primary Key 如何在Entity Framework中创建由外键组成的复合主键? - How can I create a composite primary key consisting of foreign keys to two tables in Entity Framework? 如何配置包含外键的复合主键(EF Fluent API) - How to configure a composite primary key containing a foreign key (EF Fluent API) 如何在asp.core中使用Fluent API创建关系或主键? - How to create a relationship or primary key with Fluent API in asp.core? 流利的API,将主键定义为外键 - Fluent API, define a primary key as foreign key EntityFramework外键作为主键,具有流畅的API - EntityFramework foreign key as primary key with fluent API ASP.NET MVC通过流畅的API删除外键约束 - ASP.NET MVC remove foreign key constraint via fluent API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM