In my controller, the[POST]
methods usually call a Stored Procedure from the database. I am wondering if I am doing the right thing in order to avoid concurrency issues. Take the following method as example:
[HttpPost]
public async Task<IActionResult> Create(Phase phase)
{
int releaseId = (int)TempData["id"];
string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
using (SqlConnection connection = new SqlConnection(connectionString))
{
string sql = "CreatePhase";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.CommandType = CommandType.StoredProcedure;
// adding parameters
SqlParameter parameter = new SqlParameter
{
ParameterName = "@Name",
Value = phase.Name,
SqlDbType = SqlDbType.VarChar,
Size = 50
};
command.Parameters.Add(parameter);
parameter = new SqlParameter
{
ParameterName = "@ReleaseId",
Value = releaseId,
SqlDbType = SqlDbType.Int
};
command.Parameters.Add(parameter);
connection.Open();
await command.ExecuteNonQueryAsync();
connection.Close();
}
}
return RedirectToAction("Index", "Phase", new { id = releaseId });
}
Is await command.ExecuteNonQueryAsync();
the proper way to avoid concurrency? Does it have any effect? Are there any better ways of achieving this or is this good enough?