Skip to content

Instantly share code, notes, and snippets.

@ro31337
Created August 22, 2014 12:12
Show Gist options
  • Save ro31337/988c78c5f139d3d6da52 to your computer and use it in GitHub Desktop.
Save ro31337/988c78c5f139d3d6da52 to your computer and use it in GitHub Desktop.
private void SoftDelete(DbEntityEntry entry)
{
Type entryEntityType = entry.Entity.GetType();
string tableName = GetTableName(entryEntityType);
string primaryKeyName = GetPrimaryKeyName(entryEntityType);
string sql =
string.Format(
"UPDATE {0} SET IsDeleted = 1 WHERE {1} = @id",
tableName, primaryKeyName);
Database.ExecuteSqlCommand(
sql,
new SqlParameter("@id", entry.OriginalValues[primaryKeyName]));
// prevent hard delete
entry.State = EntityState.Detached;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment