WebDevGuy2
WebDevGuy2

Reputation: 1259

SQL Server - Records being deleted randomly?

I have an ASP.NET MVC app I've developed for a client. MVC 5, EF 6.

The web server and db server are separate. All of a sudden a bunch of data was deleted from the db server. Users were performing no delete functions. Is there any scenario that would cause this? The records seem to be random. No stored procs, triggers, etc.... running. The app was working fine for months. Any scenario where SQL Server (2014 version) would delete records in a table? No errors were displayed to the user.

**** UPDATE ****

The only "delete" related code that I rolled out recently was this...

   [Authorize]
    public class WorkResultsController : Controller
    {
        private readonly ABC_WorkingContext db = new ABC_WorkingContext();

        public ActionResult DeleteEvent(int id, bool? performRedirectAfterDelete = true)
        {
            if (!WorkFormServices.IsEditOrDeleteEnabled(id)) return this.HttpNotFound();

            var @event = this.db.Events.Find(id);

            try
            {
                // first remove any Work questions related to this event
                var WorkResultsToDelete = this.db.WorkResults.Where(e => e.EventId == id).ToList();
                foreach (var row in WorkResultsToDelete) this.db.WorkResults.Remove(row);

                this.db.Events.Remove(@event);
                this.db.SaveChanges();

                if (performRedirectAfterDelete == true) return this.RedirectToAction("Index", "WorkScheduler");
                return this.Json(
                    new { success = true, responseText = "Delete success!" },
                    JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return this.Json(
                    new { success = false, responseText = "Delete failed!!" },
                    JsonRequestBehavior.AllowGet);
            }
        }

I want to delete only WorkResults records related to the specific ID. So, I believe this is working correctly. Do you see any unintended deletes that could happen?

Upvotes: 1

Views: 1746

Answers (2)

scgough
scgough

Reputation: 5252

I agree with Min - a DB won't just delete data. This is more than likely a code (app or DB side) issue or a breach of some kind.

I would check:

  • app code - is there a bad SQL call/statement (related to the tables you're missing data from) that could have deleted more than it should
  • Stored Procs, Triggers - same as above - an SQL mistake could wreak havoc
  • Table relationships - are any unwanted cascade deletes set up?
  • EF - are there any unwanted cascades set up in this between entities?
  • Logins - for sanity - change the passwords for the logins your app uses...this could be a breach maybe - hard to tell without seeing the pattern of missing data

Upvotes: 3

Min Hyoung Hong
Min Hyoung Hong

Reputation: 1202

First, no commercial DB deletes random data by itself. If it really deletes its client's data, its maker would be sued by client.
So, there are DELETE queries in somewhere or someone executed DELETE operation on SQL SERVER Studio. You can monitor DB queries. Check your queries and find which query delete your data. And ask DBA or DB Operator if they executed some queries.

In my experience, there is no "THERE IS NO SUCH QUERY".

Upvotes: 1

Related Questions