經過ef core 源碼分析 Microsoft.EntityFrameworkCore.Storage.RelationalCommandExtensions類中有相應的方法 爲此獲得相應的結果:sql
爲此在咱們的數據上下文中拓展方法:async
public async Task<DbDataReader> ExecuteReaderAsync(RawSqlString sql, params object[] parameters) { using (await Database.GetService<IConcurrencyDetector>().EnterCriticalSectionAsync(default)) { RawSqlCommand rawSqlCommand = Database.GetService<IRawSqlCommandBuilder>().Build(sql.Format, parameters); return (await rawSqlCommand.RelationalCommand.ExecuteReaderAsync(Database.GetService<IRelationalConnection>(), rawSqlCommand.ParameterValues)).DbDataReader; } } // FormattableString public Task<DbDataReader> ExecuteReaderAsync(FormattableString sql) { return ExecuteReaderAsync(sql.Format, sql.GetArguments()); } public Task<object> ExecuteScalarAsync(FormattableString sql) { return ExecuteScalarAsync(sql.Format, sql.GetArguments()); } public async Task<object> ExecuteScalarAsync(RawSqlString sql, params object[] parameters) { using (await Database.GetService<IConcurrencyDetector>().EnterCriticalSectionAsync(default)) { RawSqlCommand rawSqlCommand = Database.GetService<IRawSqlCommandBuilder>().Build(sql.Format, parameters); return await rawSqlCommand.RelationalCommand.ExecuteScalarAsync(Database.GetService<IRelationalConnection>(), rawSqlCommand.ParameterValues); } }