c# - Generic Delete in Entity Framework -
i have generic delete method dodelete<mytable>(int batchsize, mydatabasecontext)
this methods builds ids (list<int> deleteid
) upto batchsize count , call
bulkdelete<t>(mydatabasecontext, t => deleteid.contains(t.recordid))
however approach not work because unaware <t>
has property recordid
(even though recordid
property).
i have tried:
int rows = bulkdelete<t>(mydatabasecontext, "recordid in (100001,100002)");
this actual method delete, have overload accepts string predicate.
public int bulkdelete<t>(mydatabasecontext context, expression<func<t, bool>> predicate) t : class { int rows = context.set<t>().where(predicate).delete(); return rows; }
as @mason says, can create interface recordid
property entities implement, generic t=>deleteid.contains(t.recordid)
compile:
// interface public interface irecordidentity { int recordid {get;set;} } // sample class public class person : irecordidentity { // implement interface public int recordid {get;set;} } // generic bulkdelete irecordidentitys public int bulkdelete<t>(mydatabasecontext context, expression<func<t, bool>> predicate) t : irecordidentity { int rows = context.set<t>().where(predicate).delete(); return rows; } // sample call bulkdelete<person>(mydatabasecontext, t=> deleteid.contains(t.recordid))
now bulkdelete()
restricted working on types implementing irecordidentity
predicate valid.
Comments
Post a Comment