I am receiving an exception:
Unable to update the EntitySet 'Session' because it has a DefiningQuery and no element exists in the element to support the current operation.
while trying to delete a row from a database table. The exception appears when calling SaveChanges()
for the row which exists. Below is my code:
public static Func<DC21GPDEntities, string, IQueryable<Session>> compiledDeleteQuery =
CompiledQuery.Compile((DC21GPDEntities ctx, string userId) =>
(from rows in ctx.Sessions
where rows.User_ID == userId
select rows));
[HttpPost]
public ActionResult Index(string searchItem )
{
try
{
string userId =searchItem.Trim();
string successMessage
= "The session for User ID: " + userId + " has been cleared in Fascor.";
dc21gpdContext.CommandTimeout = 180;
Models.Session session = Queries.compiledDeleteQuery(dc21gpdContext, userId).FirstOrDefault();
if (session == null)
successMessage = "Session for User ID: " + userId + " does noe exist";
else
{
dc21gpdContext.DeleteObject(session);
dc21gpdContext.SaveChanges();
}
ViewData["SuccessMessage"] = successMessage;
return View();
}
catch (Exception ex)
{
ViewData["SuccessMessage"] = "Failed to clear session";
return View();
}
}