FOREIGN KEY constraint - Issue RRS feed

  • Question




    I begin to use entityFramework Code First.


    I create 4 objects: 

    Categories, Regions, Department and Announcements.


    Relations are these one:

    The relation between Regions and Department is :

    For 1 region, there is several Departement


    An Announcement is associated to only 1 region, 1 department, 1 categorie.

    So when I use EntityFramework CodeFirst by only Region/Categorie/Department, the database can be created, the controllers works etc.

    But after to add the class Announcement with the code below, each time I obtain the error during the process to create the database. Visual Studio indicates the 

    "The database creation succeeded, but the creation of the database objects did not. See InnerException for details. "

    In details:

    {"Introducing FOREIGN KEY constraint 'Region_Departments' on table 'Department' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.\r\nCould not create constraint. See previous errors."}

    \r\nCould not create constraint. See previous errors."}

    You can see below the created classes:



      public class Categorie
        [Display(Name = "CategorieID")]
        public int CategorieID { get; set; }
        [Required(ErrorMessage = "Name of categorie is required")]
        public string CategorieName { get; set; }
        [Required(ErrorMessage= "ParentId is required")]
        [Display(Name = "CategorieParentId")]
        public int CategorieParentID { get; set; }
        //public virtual ICollection<Announcement> Announcements { get; set; }
     public class Department
        [Display(Name = "Department ID")]
        [Required(ErrorMessage = "Department ID is required.")]
        public int DepartmentID { get; set; }
        [Display(Name = "Department Name")]
        [Required(ErrorMessage = "Department Name is required.")]
        public String DepartmentName { get; set; }
        [Display(Name = "Region Id")]
        [Required(ErrorMessage = "Region Id is required.")]    
        public int RegionID { get; set; }
        public virtual Region Region {get;set;}    



      public class Region
        [Display(Name = "Region Id")]
        public int RegionID { get; set; }
        [Display(Name = "Region Name")]
        public String RegionName { get; set; }
        public virtual ICollection<Department> Departments { get; set; }    

     public class Announcement
        [Display(Name = "AnnouncementID")]
        public int AnnouncementID { get; set; }
        [Display(Name = "Region Id")]
        public int RegionID { get; set; }
        [Display(Name = "Department Id")]
        public int DepartmentID { get; set; }
        [Display(Name = "Postal Code")]
        public String CodePostal { get; set; }
        [Display(Name = "Categorie Id")]
        public int CategorieID { get; set; }
        [Display(Name = "Is Professional")]
        public Boolean IsProfessional { get; set; }
        [Display(Name = "Is Offer")]
        public Boolean IsOffer { get; set; }
        [Display(Name = "Name")]
        public String Name { get; set; }
        [Display(Name = "Email")]
        public String Email { get; set; }
        [Display(Name = "Phone number")]
        public String TelephoneNumber { get; set; }
        [Display(Name = "Title")]
        public String Title { get; set; }
        [Display(Name = "Description")]
        public String Description { get; set; }
        [Display(Name = "Price")]
        public String Price { get; set; }
        public virtual Categorie Categorie { get; set; }
        public virtual Region Region { get; set; }
        public virtual Department Department { get; set; }


    So I think the part about public virtual Region/Department/Categorie defined in Announcement creates the issue when the engine of EntityFramework creates the database. Do you already encountered this issue?

    On my point of view, Announcement needs to create a FK with Categorie/Region/Department and Region needs to have a FK with Department.

    I use too during the creation the database the seed() method.

      public class MyDataContextInitializer:DropCreateDatabaseIfModelChanges<MyDataContext>
        protected override void Seed(MyDataContext context)

    If you need more details I put the database diagram https://docs.google.com/leaf?id=0B9NwsLBZK3LnYzcyMThkY2ItZTMzYi00N2MwLWFjYTItZDZjZGY4YjVhYTQ5&hl=en_US&authkey=CM-DqEU

    Best regards,



    Saturday, June 11, 2011 11:18 AM


  • Configure a custom configuration on the Department Class and set this


    On the Region property.

    Devlin Liles http://twitter.com/devlinliles http://www.devlinliles.com/ If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, June 11, 2011 5:20 PM

All replies