使用Fluent Api时Composite Key EF Core出现错误 [英] Composite Key EF Core getting error when using Fluent Api

查看:283
本文介绍了使用Fluent Api时Composite Key EF Core出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Entity Framework Core中有以下类。我正在尝试先进行代码迁移,但终生无法弄清楚如何使此工作流利的API。

So I have the following class in Entity Framework Core. I am trying to do a code first migration and can't for the life of me figure out how to make the fluent API for this work.

public class Participants
{
    public Activity Activity { get; set; } //Class with Id and Name of Activity
    public ApplicationUser Participant { get; set; } 

    [Key]
    [Column(Order = 1)]
    public int ActivityId { get; set; }


    [Key]
    [Column(Order = 2)]
    public string ParticipantId { get; set; }
}

在EF6中,我能够在OnModelCreating中执行此操作精细。

In EF6 I was able to do this in OnModelCreating to get it to work fine.

 modelBuilder.Entity<Attendance>()
            .HasRequired(a => a.Activity)
            .WithMany()
            .WillCascadeOnDelete(false);

但是在EF Core中,我得到了

But in EF Core I get


实体类型'参与者'具有使用数据注释定义的复合主键。要设置复合主键,请使用fluent API。

" Entity type 'Participants' has composite primary key defined with data annotations. To set composite primary key, use fluent API."

我尝试使用

modelBuilder.Entity<Participants>().HasKey(p => new {p.Activity, p.Participant});

但是,这只会导致


在表参与者上引入外键约束 FK_Participants_AspNetUsers_ParticipantId可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

Introducing FOREIGN KEY constraint 'FK_Participants_AspNetUsers_ParticipantId' on table 'Participants' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

如果有更好的方法可以整件事我都乐于提出建议。如果您有复数订阅,那么我基本上是想让Mosh Hamedani的成为全栈开发人员在EF core中工作。该示例位于 13-full-stack-fundamentals文件夹中。

If there is a better way to do the whole thing I'm open to suggestions. If you have pluralsight subscription, I'm basically trying to get "Become a Full Stack Developer" by Mosh Hamedani to work in EF core. The example is in "13-full-stack-fundamentals" folder.

更新:也尝试过

        modelBuilder.Entity<Participants>()
            .HasOne(p => p.Activity)
            .WithMany()
            .OnDelete(DeleteBehavior.Cascade);

仍然得到


实体类型'参与者'具有使用数据注释定义的复合主键。要设置复合主键,请使用fluent API。

"Entity type 'Participants' has composite primary key defined with data annotations. To set composite primary key, use fluent API."

更新2:在尝试了Roy的建议之后,这就是我得到的

UPDATE 2: After trying Roy's suggestion this is what I'm getting


在表'Participants'上引入FOREIGN KEY约束'FK_Participants_AspNetUsers_ParticipantId'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

Introducing FOREIGN KEY constraint 'FK_Participants_AspNetUsers_ParticipantId' on table 'Participants' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

更新3:在迁移中

我删除了OneDelete之一:ReferntialAction.Cascade,它起作用了。我从FK_Participants_AspNetUsers_ParticipantId中删除了一个。

I removed one of the OneDelete: ReferntialAction.Cascade and it worked. I removed the one off of FK_Participants_AspNetUsers_ParticipantId.

我在OnModelCreating中也对此进行了更改

I also changed to this in my OnModelCreating

        modelBuilder.Entity<Participants>()
            .HasKey(p => new { p.ActivityId, p.ParticipantId });
        base.OnModelCreating(modelBuilder);

        //Added this ( Not sure if it's needed if anyone knows let me know) 
        modelBuilder.Entity<Participants>()
            .HasOne(p => p.Activity)
            .WithMany()
            .OnDelete(DeleteBehavior.Cascade);


推荐答案

您要尝试做的是在活动和参与者在EFCore中略有不同。

What you are trying to do is create a relationship between Activity and Participant which is a little different in EFCore.

要做到这一点,您需要在模型构建器中引用ForeignKey属性而不是NavigationProperties,如下所示:

To do it, you would need to reference the ForeignKey Properties instead of NavigationProperties in the modelbuilder as follows:

    modelBuilder.Entity<Participants>()
        .HasKey(p => new { p.ActivityId , p.ParticipantId });

这篇关于使用Fluent Api时Composite Key EF Core出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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