Solved

Entity framework: Delete objects  that respects some conditions

Posted on 2010-09-20
3
829 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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 42
Save json data from URL using SSIS 1 31
Regarding swagger API 1 37
Need help with another query 10 39
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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