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