Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

remove not working with entity framework.

Posted on 2012-09-22
4
Medium Priority
?
1,199 Views
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();

            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
0
Comment
Question by:scm0sml
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 38425948
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
 

Author Comment

by:scm0sml
ID: 38425954
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
 
LVL 25

Accepted Solution

by:
apeter earned 2000 total points
ID: 38425964
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
 

Author Comment

by:scm0sml
ID: 38426118
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

578 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