c# - setting up objects for code first EF6 foreign key issue -
i have following models (pseudocode):
post { int id list<like> likes list<comment> comments } comment { int id list<like> likes int postid -- fk post post post } { int id int commentid -- fk comment comment comment int postid -- fk post post post }
the important bit here like
has foreign key both post
, comment
, because each post , comment can have own instance (liking post vs liking comment).
entity framework complaining multiple cascade paths, what's correct way build these entities?
if delete post delete likes , comments. likes delete comments.
multiple cascade paths because deleting comments twice.
you can either edit generated code first code , change "cascadedelete:true" false or can fix classes.
you should have likecomment class.
public class comment { public int id { get; set; } public list<commentlike> likes { get; set; } public int postid { get; set; } public comment() { likes = new list<commentlike>(); } } public class { public int id { get; set; } }
public class post { public int id { get; set; } public list<postlike> likes { get; set; } public list<postcomment> comments { get; set; } public post() { likes = new list<postlike>(); comments = new list<postcomment>(); } } public class postcomment : comment { public int postid { get; set; } [foreignkey("postid")] public virtual post post { get; set; } } public class likecomment : comment { public int likeid { get; set; } [foreignkey("likeid")] public virtual like { get; set; } } public class postlike : { public int postid { get; set; } [foreignkey("postid")] public virtual post post { get; set; } } public class commentlike : { public int commentid { get; set; } [foreignkey("commentid")] public virtual comment comment { get; set; } }
Eduwizz online training is one of the Best Online Training Institute in Hyderabad, Bangalore.Eduwizz provide courses Hybris, Machine Learning , AWS, Statistics for Beginners, Commvault Training,Devops, Netapps,
ReplyDeleteData Science , Internet of Things , IBM Blue-Mix , Hybris ,Angular JS , Node JS , Express JS , Business Analyst, Selenium testing with webdriver, Guidewire ,Adobe, RPA ,TSM, EMC...etc