EF Does not execute delete command

I use an entity structure as a data access layer. But for some tasks (like removing a package) I want to use raw SQL commands.

But at this point, EF surprises me because it seems to be performing DML, but does nothing.

If I run sql directly, it executes and deletes the data as expected. So what am I doing wrong?

    using (var db = new MyDbContext())
    {
        var sqlTime = string.Concat("DELETE FROM SYNCING_CONTENT WHERE ID in (SELECT SyncContent_Id FROM SYNCING WHERE EXPIRATION < GETDATE());");
        var deleted = db.Database.ExecuteSqlCommand(sqlTime);

        return deleted;
    }

EDIT:

    using (var db = new MyDbContext())
    {
        const string SQL_TIME = "DELETE FROM SYNCING_CONTENT WHERE ID in (SELECT SyncContent_Id FROM SYNCING WHERE EXPIRATION < GETDATE())";
        var deleted = db.Database.ExecuteSqlCommand(SQL_TIME);

        return deleted;
    }
+3
source share
1 answer

Try to delete; from your SQL string.

Also, the problem may be related to passing GETDATE () in your sql string, I know that this is a problem if you pass it as a sql parameter.

Try

const string SQL_TIME = @"DELETE FROM SYNCING_CONTENT WHERE ID in (SELECT SyncContent_Id FROM SYNCING WHERE EXPIRATION < {0})";
db.Database.ExecuteSqlCommand(SQL_TIME, DateTime.Now);
+1
source

All Articles