首先使用EF Core 2.0代码将2个外键作为主键 [英] 2 Foreign Keys as Primary Key using EF Core 2.0 Code First
问题描述
我有两个表:评论和赞。
public class Comment {
CommentID { get; set; }
....
}
public class Like {
CommentID { get; set; }
UserID { get; set; }
}
首先使用实体框架核心2.0代码,我想定义 模型,因为它仅具有引用其他主键(作为外键)的两个字段,但我也希望值的组合成为表的主键。任何帮助将不胜感激!
Using entity framework core 2.0 code first, I want to define my "Like" model as only having the two fields which reference other primary keys (as foreign keys) but also I want the combination of the values to be the Primary Key of the table. Any help would be appreciated!
推荐答案
因此,这是这里发生的事情:
So, here's what's going on here:
1)有两个类别:评论
和 User
。
1) There are two classes: Comment
and User
.
2)第三个类 Like
保存对两个对应于数据库中外键的两个类的引用(导航属性): UserId
和 CommentId
。我显式地使用了 ForeignKey
属性,以便您可以清楚地知道EF将使用哪些属性作为外键。在这种特殊情况下,您可以忽略此属性,因为EF会自动找出它(因为两个类中的名称都匹配)。请注意,具有外键不是强制性的,但它们具有优势。
2) The third class, Like
, holds references (navigation properties) to both two those classes which correspond to foreign keys in database: UserId
and CommentId
. I explicitly used ForeignKey
attribute, so that it would be clear to you which properties EF will use as foreign keys. In this particular case you could omit this attribute since EF will figure it out automatically (since names match in both classes). Note that it's not mandatory to have foreign keys, but they have advantages.
3) UserId
和 CommentId
包含组合键。 Column
属性配置数据库中列的顺序(所谓的序数)。
3) The UserId
and CommentId
comprise composite key. The Column
attribute configures the order of columns in database (so called ordinals). This is important for EF.
4) User
和 Comment
类还具有导航属性(因为它是一对多
关系的一个
一侧):喜欢。
4) The User
and Comment
classes also have navigation properties (since it's one
side of one-to-many
relations): Likes
.
5)最后,总是
使用 Table
属性可避免出现复数问题,因为没有办法将其关闭。
5) Finally, always
use Table
attribute to avoid problems with pluralizations because there's no way to turn it off.
[Table("Comment")]
public class Comment
{
public int CommentID { get; set; }
public List<Like> Likes { get; set; }
}
[Table("User")]
public class User
{
public int UserId { get; set; }
public List<Like> Likes { get; set; }
}
[Table("Like")]
public class Like
{
[Key]
[Column(Order = 1)]
public int CommentID { get; set; }
[Key]
[Column(Order = 2)]
public int UserID { get; set; }
[ForeignKey("CommentId")]
public Comment Comment { get; set; }
[ForeignKey("UserId")]
public User User { get; set; }
}
更新
在EF Core中设置复合键
键
(和 Column
)属性(用于指定复合主键)实际上在EF Core中不起作用-它们在EF6中起作用。要在EF Core中配置组合键,您需要使用Fluent Configuration。
The Key
(and Column
) attributes, used to designate composite primary key, actually, don't work in EF Core - they work in EF6. To configure composite key in EF Core, you need to use Fluent Configuration.
您有两种选择。
选项1。在 OnModelCreatingMethod
中进行所有配置:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Like>().HasKey(l => new { l.CommentID, l.UserID });
}
选项2。将所有配置移到单独的位置类并将其应用于 OnModelCreating
:
OPTION 2. Move all the configuration into separate class and apply it in OnModelCreating
:
1)创建用于配置的单独类
1) Create separate class for configuration
class LikeConfiguration : IEntityTypeConfiguration<Like>
{
public void Configure(EntityTypeBuilder<Like> builder)
{
builder.HasKey(l => new { l.CommentID, l.UserID });
}
}
2)应用配置:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new LikeConfiguration());
}
选择任何您喜欢的选项。
Choose any option you like.
如您所见,要在Fluent Configuration中配置组合键,您需要使用匿名类型。同样,属性的顺序很重要。
As you see, to configure composite key in Fluent Configuration, you need to use anonymous type. And again, the order of properties matters.
这篇关于首先使用EF Core 2.0代码将2个外键作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!