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();


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
Who is Participating?
Have you enabled cascade delete in your tables? Check below link for more info.
This blog row which you are deleting has a reference in some other table. So you to delete them first and delete this blog
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?
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.
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.