?
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
?
73 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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