remove not working with entity framework.

I am using entity framework and have the following:
var id = Convert.ToInt32(ddBlogs.SelectedValue);

            var blog = (from b in db.Blogs
                        where b.ID == id                        
                        select b).FirstOrDefault();

            db.Blogs.Remove(blog);
            db.SaveChanges();  

Open in new window


blog has a list of BlogComments as in blog.BlogComments() and when I try to do the remove I am getting:
The DELETE statement conflicted with the REFERENCE constraint \"FK__BlogComme__BlogI__1DE57479\". The conflict occurred in database \"MyDatabase\", table \"dbo.BlogComments\", column 'BlogID'.\r\nThe statement has been terminated."}

How can I get this delete to cascade
scm0smlAsked:
Who is Participating?
 
apeterCommented:
Have you enabled cascade delete in your tables? Check below link for more info.
http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx
0
 
apeterCommented:
This blog row which you are deleting has a reference in some other table. So you to delete them first and delete this blog
0
 
scm0smlAuthor Commented:
But cant i get it to do that automatically?

Blog is blogcomments parent, surely i should be able to get this delete to cascade?
0
 
scm0smlAuthor Commented:
OK great thanks for your link there.

Including the blogcomments in the initial load meant they could be deleted.

As it is a simple application this will work fine but for a larger size app I would have created the cascade rule on the db.

Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.