Loops and multiple cascading paths using EF Code First

I'm so confused !!! This is my first time using MVC3 and EF Code First. I get the following error:

Representation of the FOREIGN KEY constraint "FK_dbo.CityUsers_dbo.Cities_CityID" in the "CityUsers" table can cause loops or several cascading paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION or change other FOREIGN KEY constraints. Failed to create constraint.

I worked on this watch and did everything I could, but just could not handle it!

The scenario is that there is a class Man from which the User is derived. Each user belongs to only one city. By user I mean administrators. Each of them can insert / update several cities, and each City can be changed by only one user at a time. I created a third table called "CityUser" to track users who modify City entries.

Here are my model classes:

public class Man { [Key] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public long ID { get; set; } //------------------------------------------------------------// [Required, MaxLength(20)] [LocalizedAttribute("FName")] public string FName { get; set; } //------------------------------------------------------------// [Required, MaxLength(20)] [LocalizedAttribute("LastName")] public string LastName { get; set; } //------------------------------------------------------------// [Required] [RegularExpression("^[0-9]+$", ErrorMessageResourceName = "ErrorOnlyNumbers", ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources))] [LocalizedAttribute("Mobile")] public string Mobile { get; set; } //------------------------------------------------------------// [LocalizedAttribute("Phone")] [RegularExpression("^[0-9]+$", ErrorMessageResourceName = "ErrorOnlyNumbers", ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources))] public string HomePhone { get; set; } //------------------------------------------------------------// [RegularExpression("^[0-9]+$")] [LocalizedAttribute("IDCardNumber")] public string IDCardNumber { get; set; } //------------------------------------------------------------// [RegularExpression("^[0-9]+$")] [LocalizedAttribute("NationalCode")] public string NationalCode { get; set; } //------------------------------------------------------------// [MaxLength(10)] [LocalizedAttribute("DOB")] public int DOB { get; set; } //------------------------------------------------------------// [Required] public int CityID { get; set; } [ForeignKey("CityID")] public virtual City CityParent { get; set; } //------------------------------------------------------------// [MaxLength(100)] [LocalizedAttribute("Address")] public string Address { get; set; } //------------------------------------------------------------// [LocalizedAttribute("PostalCode")] public string PostalCode { get; set; } //------------------------------------------------------------// [MaxLength(255)] [LocalizedAttribute("PhotoPath")] public string PhotoPath { get; set; } } public class User : Man { [MaxLength(20)] [LocalizedAttribute("Username")] public string UserName { get; set; } //------------------------------------------------------------// [DataType(DataType.Password)] [MaxLength(100), MinLength(6, ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources), ErrorMessageResourceName = "ErrorPasswordLength")] [LocalizedAttribute("Password")] public string Password { get; set; } //------------------------------------------------------------// [DataType(DataType.Password)] [Compare("Password", ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources), ErrorMessageResourceName = "ErrorConfirmPassword")] [LocalizedAttribute("ConfirmPassword")] public string ConfirmPassword { get; set; } //------------------------------------------------------------// [DataType(DataType.EmailAddress, ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources), ErrorMessageResourceName = "ErrorEmailInvalid")] [MaxLength(20)] [RegularExpression(@"[a-zA-Z0-9._%+-] +@ [a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")] [LocalizedAttribute("Email")] public string Email { get; set; } //------------------------------------------------------------// [MaxLength(30)] [LocalizedAttribute("Title")] public string Title { get; set; } //------------------------------------------------------------// [MaxLength(10)] [LocalizedAttribute("HireDate")] public int HireDate { get; set; } //------------------------------------------------------------// [LocalizedAttribute("ReportsTo")] public long ReportsTo { get; set; } [ForeignKey("ReportsTo")] public virtual IList<User> ReportsChild { get; set; } } public class City { [Key] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public int CityID { get; set; } //------------------------------------------------------------// [Required, MaxLength(20)] [LocalizedAttribute("Name")] public string Name { get; set; } //------------------------------------------------------------// [LocalizedAttribute("PhoneCode")] public int PhoneCode { get; set; } //------------------------------------------------------------// [Required, MaxLength(10)] public int ModifiedDate { get; set; } } public class CityUser { [Key] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public int CityUserID { get; set; } //------------------------------------------------------------// [Required] public long ModifiedByUserID { get; set; } [ForeignKey("ModifiedByUserID")] public virtual User OperatorUser { get; set; } //------------------------------------------------------------// [Required] public int CityID { get; set; } [ForeignKey("CityID")] public virtual City City { get; set; } } 

But as EF5 goes to create the database, the error mentioned above appears! What can I do for this? I read so many weblogs that I made some changes to the data model. But still I can’t deal with this error ... By the way, I want to declare a relationship using DataAnnotations .

Now there is someone who can save me from this problem ??? !!!!: (

Hello,

+4
source share
1 answer

Finally, I could come through this after several hours, and with a lot of effort, and thus will never and never forget the concept !!!: D Solution found:

 modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 
+4
source

Source: https://habr.com/ru/post/1482739/


All Articles