Mashudu M
Mashudu M

Reputation: 31

Await within a waited dbcontext, should one use SaveChangesAsync on a context that is waited on using or SaveChanges() is enough

If a dbcontext is waited on using should one still use SaveChangesAsync() or SaveChanges() should be enough? await using var dbContxt = new dbContxt(con);

public async Task<bool> SaveData(strig con, object data)
{
    await using var dbContxt = new dbContxt(con);

    var myData = new MyData
    {
        a = data.a,
        b = data.b,
        c = data.c,
    };

    dbContxt.MyData.Add(myData);
    var result = await dbContxt.SaveChangesAsync();
    return result > 0;
    }
}

Upvotes: 0

Views: 64

Answers (1)

HGMamaci
HGMamaci

Reputation: 1386

Short answer yes. In the scope of your async Task the DbContext will be alive.

Upvotes: 0

Related Questions