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
57 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 33

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 33

Expert Comment

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

Sara
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net See list of pdf documents in folder and read 2 28
JavaScript error 1 52
Two different visual studio versions 3 21
Angular JS Route 3 44
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

939 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

9 Experts available now in Live!

Get 1:1 Help Now