entity framework - Deleting an object with SQL and updating the Context -
i using ef , have context have deleted row in table using simple sql call through executestorecommand function on context (i have way other reasons). works fine, context doesn't know happened.
i problem later when using same context when i'm trying commit changes because commit not give expected number of affected rows.
my question is, best way update context changes (deleted rows) i've made.
i've tried getting deleted object , using in context refresh function, doesn't work, because (correctly) gets null reference when trying deleted object.
_ctx.refresh(refreshmode.storewins, _ctx.employees.firstordefault(s => s.employeeid == employeeid)); also, using objectstatemanager.getobjectstateentries doesn't work me either, because doesn't know objects have been deleted.
i not want to:
- recreate context.
- refresh more need to.
- mess lazy loading.
i want context date again after deleting.
try (you must somehow identify deleted entity - if don't know how done , solution new context):
var employee = ctx.objectstatemanager.getobjectstateentries(~entitystate.detached) .where(e => !e.isrelationship) .select(e => e.entity) .oftype<employee>() .firstordefault(e => e...); if (employee != null) ctx.detach(employee); btw. don't use direct sql modification attached entities. worst operation can do. ef doesn't expect , not able handle it. best solution in such case recreating context.
Comments
Post a Comment