Solved

Entity framework: Delete objects  that respects some conditions

Posted on 2010-09-20
3
820 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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