Link to home
Start Free TrialLog in
Avatar of scm0sml
scm0sml

asked on

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
Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

This blog row which you are deleting has a reference in some other table. So you to delete them first and delete this blog
Avatar of scm0sml
scm0sml

ASKER

But cant i get it to do that automatically?

Blog is blogcomments parent, surely i should be able to get this delete to cascade?
ASKER CERTIFIED SOLUTION
Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of scm0sml

ASKER

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.