简体   繁体   中英

ITable<T>.DeleteAllOnSubmit() is absent?

As far as I see ITable<T> interface doesn't have DeleteAllOnSubmit() method that exists in ITable interface and in Table<T> class.

I am going to implement on my own something like this:

public static void DeleteAllOnSubmit<T>(this ITable<T> table, IEnumerable<T> entities)
{
    entities.ForEach(entity=>table.DeleteOnSubmit(entity);
}

Question 1: Is there any pitfalls here? If it was so easy Microsoft would implement that thierselves...

Question 2: Why that was not implmemented out-of-the box?

Having a look at the implementation of Table<TEntity>.DeleteAllOnSubmit<TSubEntity>() shows, that there isn't much more going on:

public void DeleteAllOnSubmit<TSubEntity>(IEnumerable<TSubEntity> entities) where TSubEntity: TEntity
{
    if (entities == null)
    {
        throw Error.ArgumentNull("entities");
    }
    this.CheckReadOnly();
    this.context.CheckNotInSubmitChanges();
    this.context.VerifyTrackingEnabled();
    foreach (TEntity local in entities.ToList<TSubEntity>())
    {
        this.DeleteOnSubmit(local);
    }
}

So, I'd say: Your implementation is just fine. BTW: There is no ForEach extension method for IEnumerable<T> .

Alternatively you could implement something to the effect of..

public static void DeleteAllOnSubmit<T>(this ITable<T> table, IEnumerable<T> entities)
{
    table.Context.GetTable<T>().DeleteAllOnSubmit(entities);
}

Just traveling through the ITable<T> to the context specific table and calling its DeleteAllOnSubmit() . That way there isn't the deviation in the way you enumerate and delete all and the way the context does natively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM