Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
76 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 35

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 35

Expert Comment

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

Sara
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 35

Accepted Solution

by:
sarabande earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

610 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