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
61 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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