?
Solved

Entity framework: Delete objects  that respects some conditions

Posted on 2010-09-20
3
Medium Priority
?
833 Views
Last Modified: 2013-11-11
Hi!

I only want to know if this is possible to delete, with Entity Framework 4.0 some objects directly by specifying conditions.

I know I can select all object and then do a foreach on them and delete them. the goal is to avoid to retrieve data only to delete them. So please don't provide me a solution like

foreach( MyObjectType myObject in DataContext.MyObjectType.Where( <conditionsHere>))
   DataContext.MyObjectType.DeleteObject(myObject);
myObject.SaveChanges();

The goal is to have something like:

DataContext.MyObjectType.Delete.Where(m=> m.date<= someOtherDate);
0
Comment
Question by:Nargzul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 33717155
Hi Nargzul;

To your question, "I only want to know if this is possible to delete, with Entity Framework 4.0 some objects directly by specifying conditions.", If by directly you mean entities NOT in the ObjectContext but only show up in the database then NO. In order for the ObjectContext to delete an entity it must have its Key and to have its Key means it is in the ObjectContext.

Fernando
0
 
LVL 1

Author Closing Comment

by:Nargzul
ID: 33717344
Thank you, but I find this limitation stupid :s
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33717547
You could execute an SQL comment directly by using the connection of the ObjectContext but it must be a correctly formed SQL syntax because the command is not checked at compile time but only at run-time.

ObjectContextInstance.ExecuteStoreCommand("SQL Statemens as a string", params Object[] parameters);

Documentation ObjectContext.ExecuteStoreCommand Method :
http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.executestorecommand.aspx

0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

762 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