简体   繁体   中英

How do I do an EF Database.ExecuteSQLCommand async?

Here's the code that I am using:

    public async Task<IHttpActionResult> NewTopicTests([FromBody] NewTopicTestsDTO testSpec)
    {
        var sql = @"dbo.sp_new_topic_tests @Chunk";
        SqlParameter[] parameters = new SqlParameter[]
                    {
                        new SqlParameter("@Chunk", testSpec.Chunk)
                    };
        int result = db.Database.ExecuteSqlCommand(sql, parameters);
        await db.SaveChangesAsync();
        return Ok();
    }

Can someone confirm if this is the correct way to do this using async? In particular do I need to do this:

        int result = db.Database.ExecuteSqlCommand(sql, parameters);
        await db.SaveChangesAsync();

Note that the code works however I am having a problem with my application where it suddenly stops without any error message. I am looking into every possible problem.

What's being saved here ? I think there is no need to call save changes here.

Remove save changes and you will see the same behavior, because any changes you've made in your stored procedure, are not tracked by entity framework context.

And you can rewrite your code as following:

int result = await db.Database.ExecuteSqlCommandAsync(sql, parameters);

Have you checked every where to find the reason of your problem ? Windows Error Log etc ?

Go to Debug menu of Visual Studio IDE, and open Exceptions, then check both 'throw' and 'user_unhandled' for 'Common Language Runtime Exceptions' and test your code again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM