Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 697
Samantha J T Star Avatar asked Dec 26 '22 03:12

Samantha J T Star


1 Answers

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.

like image 81
Yaser Moradi Avatar answered Jan 06 '23 01:01

Yaser Moradi