question

user5879-2331 avatar image
0 Votes"
user5879-2331 asked DuaneArnold-0443 commented

Error when creating Database using Entity Framework Core

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.


dotnet-aspnet-core-generaldotnet-entity-framework-core
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You don't have basic DBA skills that all software developers should have in working with a relational database and go make the tables using foreign keys manually using SSMS (SQL Server Management Studio) yourself? Then you can just scaffold the EF model from the existing database. By using SSMS, you would have known immediately when trying to configure the tables for parent/child relationship. if something was wrong.

https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships?view=sql-server-ver15

Theses are basic skills a software developer should know, like how to create a database, how to create tables and how to create relationships between tables.

IMHO, EF is not a DBA tool.

0 Votes 0 ·

0 Answers