Solved

Entity framework: Delete objects  that respects some conditions

Posted on 2010-09-20
3
815 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 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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
Comment Utility
Thank you, but I find this limitation stupid :s
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now