Entity Framework Extension

S

shapper

Hello,

I created an Entity Framework Extension that deletes a collection:

public static void DeleteObjects<TEntity>(this ObjectContext
context, EntityCollection<TEntity> collection) where TEntity : class,
IEntityWithRelationships {

if (!collection.IsLoaded) collection.Load();
while (collection.Any()) {
var entity = collection.First();
context.DeleteObject(entity);
}

} // DeleteObjects

To use this I need to do something like:

Document document = (from d in context.Documents select d).First
();
context.DeleteObjects<Subject>(document.Subjects);

But this wasn't exactly what I am looking.
I would like to somehow extend the DeleteObject to perform
DeleteObjects like:

context.DeleteObjects(entities)

How can I do this?

Thanks,
Miguel
 
A

Alberto Poblacion

shapper said:
I would like to somehow extend the DeleteObject to perform
DeleteObjects like:

context.DeleteObjects(entities)

How can I do this?

I may be misunderstanding what you are trying to do, but if your
"entities" argument is a collection of entities, why not just loop on the
items of the collection and call DeleteObject on each one?

public static void DeleteObjects(this ObjectContext
context, IEnumerable entities)
{
foreach (object entity in entities)
{
context.DeleteObject(entity);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top