I need to work with stored procedures, with the help of Entity Framework Core. This is my function:
public async Task<bool> CheckIfUserRegistered(string phoneNumber, DateTime dateOfBirth)
{
if (string.IsNullOrWhiteSpace(phoneNumber))
{
return false;
}
using (var cmd = _dbContext.Database.GetDbConnection().CreateCommand())
{
cmd.CommandText = "dbo.CheckIfUserRegistered";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@phoneNumber", SqlDbType.NVarChar) { Value = phoneNumber });
cmd.Parameters.Add(new SqlParameter("@dateOfBirth", SqlDbType.Date) { Value = dateOfBirth });
cmd.Parameters.Add(new SqlParameter("@registered", SqlDbType.Bit) { Direction = ParameterDirection.Output });
if (cmd.Connection.State != ConnectionState.Open)
{
cmd.Connection.Open();
}
await cmd.ExecuteNonQueryAsync();
return (bool)cmd.Parameters["@registered"].Value;
}
}
I'm not sure, with the correctness of this function, is this the right way to work with stored procedures in EF Core? Don't I have problems with forgotten connections, memory leakings, etc?
CheckIfUserRegisteredAsync
, since appendingAsync
to the end of an asynchronous method is a popular practice. You will see this pattern used a lot in the .NET Framework for example e.g.ExecuteNonQueryAsync
. – Jason Evans Dec 2 at 16:10