remove not working with entity framework.

Posted on 2012-09-22
Last Modified: 2012-09-23
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
Question by:scm0sml
    LVL 25

    Expert Comment

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

    Author Comment

    But cant i get it to do that automatically?

    Blog is blogcomments parent, surely i should be able to get this delete to cascade?
    LVL 25

    Accepted Solution

    Have you enabled cascade delete in your tables? Check below link for more info.

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    737 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

    18 Experts available now in Live!

    Get 1:1 Help Now