开发者

EF Does not execute delete command

I'm using the entity framework as my data access layer. But for some tasks (e.g. batch deleting), I want to use raw SQL commands.

But at this point EF surprises me, because it seems to execute the DML, but does not do anything.

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

    using (var db = new MyDbContext())
    {
        var sqlTime = string.Concat("DELE开发者_开发技巧TE 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;
    }


Try removing the ; from your SQL string.

Also the issue may be due to the passing of GETDATE() in your sql string, I know this an issue 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜