Solved

Introducing FOREIGN KEY constraint 'FK_dbo.Votes_dbo.Nodes_NodesId' on table 'Votes' may cause cycles or multiple cascade paths

Posted on 2014-02-19
7
53 Views
Last Modified: 2016-07-11
Hi all,i'm trying to create database using code first.I'm using aspnet web api 2,ef 6 andvs studio 2013.These are my classes :

public class Comment
    {
        
        public virtual int CommentId { get; set; }
        public virtual string Content { get; set; }
        public virtual int? LikesCount { get; set; }
        public virtual Int64? Created { get; set; }
        public virtual int NodesId { get; set; }
        public virtual string UsersId { get; set; }
        public ICollection<Votes> Votes { get; set; }
        public Nodes Nodes { get; set; }
        public ApplicationUsers Users { get; set; }
    }

public class Nodes
    {
        public virtual int NodesId { get; set; }
        public virtual string Type { get; set; }
        public virtual string Subject { get; set; }
        public virtual string Content { get; set; }
        public virtual int? GroupsId { get; set; }
        public virtual int? LikesCount { get; set; }
        public virtual Int64? Created { get; set; }
        public virtual Int64? Edited { get; set; }
        public virtual string UsersId { get; set; }
        //public virtual int UsersId { get; set; }
        public Groups Groups { get; set; }
        public ApplicationUsers Users { get; set; }
        public ICollection<Votes> Votes { get; set; }
    }

public class Votes
    {   
        public virtual int Id { get; set; }
        public virtual int NodesId { get; set; }
        public Nodes Nodes { get; set; }
        public virtual int CommentId { get; set; }
        public Comment Comment { get; set; }
        public virtual string UsersId { get; set; }
        public ApplicationUsers Users { get; set; }   
    }

public class ApplicationUsers : IdentityUser
    {
        public virtual string Email { get; set; }
       
        public int FriendId { get; set; }

        public ICollection<Nodes> Nodes { get; set; }
        public ICollection<Groups> Groups { get; set;}
        public ICollection<GroupsMember> GroupsMember { get; set; }
        public ICollection<Comment> Comments { get; set; }
        public ICollection<Votes> Votes { get; set; }
        public ICollection<Profiles> Profiles { get; set; }
     }

Open in new window


Fluent api

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {  
   modelBuilder.Entity<Votes>()
  .HasRequired(n => n.Nodes)
  .WithMany()
  .HasForeignKey(i => i.NodesId)
  .WillCascadeOnDelete(false);

  modelBuilder.Entity<Votes>()
 .HasRequired(n => n.Comment)
 .WithMany()
 .HasForeignKey(i => i.CommentId)
 .WillCascadeOnDelete(false);
            
  modelBuilder.Entity<Votes>()
 .HasRequired(n => n.Users)
 .WithMany()
 .HasForeignKey(i => i.UsersId)
 .WillCascadeOnDelete(false);

            base.OnModelCreating(modelBuilder);
}

Open in new window


When i run the application,it shows error :

"ExceptionMessage":"Introducing FOREIGN KEY constraint 'FK_dbo.Votes_dbo.Nodes_NodesId' on table 'Votes' 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

Open in new window


What is the problem that cause the error?How should i make it work as expected?Thanks a lot guys. :)

Here is the codes :

Codes
0
Comment
Question by:yonghan79
  • 3
  • 3
7 Comments
 
LVL 32

Expert Comment

by:sarabande
ID: 39870672
a 'foreign key constraint' would be violated if you add a value to a foreign key column where there is no reference entry in the related table. as the 'Votes' table has 3 foreign keys it would be either 'commentid' that doesn't exist in comments table or 'nodesid' which is missing in nodes table or 'userid' which is not in users table.

from the exception it is the 'nodesid' which is not valid.

note, at insert all these 3 fields must be provided with valid contents cause they were mandatory fields. with the current definition you can't let any of these fields empty (nul value).

Sara
0
 

Author Comment

by:yonghan79
ID: 39870729
Hi sara,i'm using ef code first,the error shows when i run the application and register an user.do you have any suggestion for me?thank you.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 39870950
can you post the code executed before the exception was thrown?

Sara
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:yonghan79
ID: 39871106
The application was based on asp.net web api,so i use json to register the user like this :

{"Username":"testing",password":"testing","confirmpassword":"testing","email":yonghan79@gmail.com"}
0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 39873336
the error occurred because of class Votes, which has members of Comment, Nodes, and ApplicationUsers. if you delete a  Votes object delete would delete the member objects in the other tables as well what is conflicting with the foreign key definitions of the ID members.

I don't know your design but I think you should store only the ID to the other tables and not member objects. otherwise if you create a Votes object automatically a new comment, node and user object would created, what especially for the user rarely seems to make sense. additionally you have the ID fields which could point to other objects of those 3 tables.

Sara
0
 

Author Comment

by:yonghan79
ID: 39873432
Ok,i'll try to recheck the codes.thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now