c# - Insert/Delete/Update inside a dynamic table name in mvc using Entity Framework -
i have jsonresult code each insert/update/delete below in controller. want ask possible if pass id , tablename update , delete tablename passed. can write insert query in way?
so jsonresult this:-
public jsonresult deletefromtable() { int idtodelete = convert.toint32(request.form["itemid"]); string mytablename = convert.tostring(request.form["tblname"]); try { type tabletype = typeof(coursedesc); switch (mytablename) { case "coursetbl": tabletype = typeof(coursetbl); break; case "coursedesctbl": tabletype = typeof(coursedesc); break; case "coursesubdesc": tabletype = typeof(coursesubdesc); break; case "interntbl": tabletype = typeof(internship); break; case "contacttbl": tabletype = typeof(contact); break; } using (ebcontext db = new ebcontext()) { // want add insert/update/delete code here. return new jsonresult { data = results, jsonrequestbehavior = jsonrequestbehavior.allowget }; } } catch(exception ex) { string innermessage = (ex.innerexception != null) ? ex.innerexception.message : ""; return new jsonresult { data = "not found", jsonrequestbehavior = jsonrequestbehavior.allowget }; } }
what cannot figure out how write ef query insert/update/delete. please help!!
i have approach case: first create generic class :
internal class utilitymethods<tentity, tid> tentity : class { public static bool insertentity(entities dbcontext, tentity entity) { dbcontext.entry(entity).state = entitystate.added; dbcontext.set<tentity>().add(entity); dbcontext.savechanges(); return true; } }
then use static method based on class dealing like:
case "coursetbl": results = utilitymethods<coursetbl, int>.insertentity(dbcontext, myobject); break;
Comments
Post a Comment