How do I do an EF Database.ExecuteSQLCommand async?

asp.net asp.net-web-api entity-framework entity-framework-6 sql-server

Question

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.

1
3
10/18/2014 4:26:26 AM

Accepted Answer

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.

8
10/18/2014 4:56:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow