Error when creating Database using Entity Framework Core

user 5879 1 Reputation point
2021-06-08T07:56:30.62+00:00

I am getting the error bellow when I try to create my database using Entity Framework Core:

Introducing FOREIGN KEY constraint 'FK_StudentAnnouncements_Students_StudentId' on table 'StudentAnnouncements' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.

I have a one to many relationship between Student and StudentAnnouncements. My Student entity has the code below:

public class Student
    {
        public int Id { get; set; }
        public virtual ICollection<StudentAnnouncement> StudentAnnouncements { get; set;}
    }

My StudentAnnouncement entity has the code below:

public class StudentAnnouncement
    {
        public int Id { get; set; }
        public int StudentId { get; set; }
        public virtual Student Student { get; set; }
    }

When I create a migration and update the database using [dotnet ef database update] I get the error.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
779 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,815 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.