Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11661

Entity Framework code-first: introducing foreign key constraint may cause cycles or multiple cascade paths

$
0
0

I am writing an ASP.NET MVC web app and I keep getting this error when generating the database from Entity Framework code-first (this is my first time posting on Stackoverflow so I apologize if I didn't explain the problem well in my post).

The error message is as follows:

Failed executing DbCommand (281ms) [Parameters=[],CommandType='Text', CommandTimeout='30']

CREATE TABLE [Etudiant] ( [Id] int NOT NULL IDENTITY, [Nom]nvarchar(30) NOT NULL, [Prenom] nvarchar(30) NOT NULL, [DepartementId]int NOT NULL, [FiliereId] int NOT NULL, [Cin] nvarchar(max) NOT NULL,[Password] nvarchar(max) NOT NULL, CONSTRAINT [PK_Etudiant] PRIMARYKEY ([Id]), CONSTRAINT [FK_Etudiant_Departement_DepartementId] FOREIGNKEY ([DepartementId]) REFERENCES [Departement] ([Id]) ON DELETECASCADE, CONSTRAINT [FK_Etudiant_Filiere_FiliereId] FOREIGN KEY([FiliereId]) REFERENCES [Filiere] ([Id]) ON DELETE CASCADE );

Introducing FOREIGN KEY constraint 'FK_Etudiant_Filiere_FiliereId' ontable 'Etudiant' may cause cycles or multiple cascade paths. SpecifyON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGNKEY constraints. Could not create constraint or index. See previouserrors.**

The error message mentions two entities that I have which are Etudiant and Filier.

My Filier entity contains many Etudiant entities, every Etudiant should have a Filier associated with it.

This is my Filier entity:

using System.ComponentModel.DataAnnotations;namespace Scolarité.Models{    public class Filiere    {        public int Id { get; set; }        [Required(ErrorMessage = "Libellé filiére Obligatoire")]        [StringLength(100, MinimumLength = 3)]        [Display(Name = " Libelle Filiére")]        public string LibelleF { get; set; }        public int DepartementId { get; set; }        public virtual Departement Departement { get; set; }        public virtual ICollection<Etudiant>? Etudiants { get; set; }        public virtual ICollection<Unite>? Unite { get; set; }        public virtual ICollection<Semestre>? Semestre { get; set; }    }}

This is my Etudiant entity:

using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;namespace Scolarité.Models{    public class Etudiant    {        public int Id { get; set; }        [Required(ErrorMessage = "Nom Obligatoire")]        [StringLength(30, MinimumLength = 3)]        public string Nom { get; set; }        [Required(ErrorMessage = "Prenom Obligatoire")]        [StringLength(30, MinimumLength = 3)]        public string Prenom { get; set; }        [Display(Name = " Departement")]        [ForeignKey("DepartementId")]        public int DepartementId { get; set; }        public virtual Departement Departement { get; set; }        [Display(Name = " Filiére")]        [ForeignKey("FiliereId")]        public int FiliereId { get; set; }        [ForeignKey("FiliereId")]        public virtual Filiere? Filiere { get; set; }        [Required(ErrorMessage = "CIN Obligatoire")]        [RegularExpression(@"^\d{8}$", ErrorMessage = "Le CIN doit comporter 8 chiffres.")]        public string Cin { get; set; }        [Required(ErrorMessage = "Mot de passe Obligatoire")]        [DataType(DataType.Password)]        public string Password { get; set; }    }}

What am I doing wrong? How can I fix this ? Thank you in advance!


Viewing all articles
Browse latest Browse all 11661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>