![]() ![]() Your COMMIT command is also NOT executed if timeout occurred before it is reached (and often is). It is untrappable which means execution is NOT transferred to the “BEGIN CATCH” TSQL block. When timeout occurs, execution of the TSQL stops immediately. 0 means wait indefinitely, no timeout will be raised. If we ommit setting CommandTimeout property, the default of 30 seconds will be used. But, do we handle it properly? And what exactly is happening when the command timeout occurs? SqlCommand class has CommandTimeout property (do not mix it with the ConnectionTimeout which stops long login process). ( this DatabaseFacade database, string query, Func map,Ĭ Data Provider for SQL Server ( namespace in assembly) has two common classes we often use: SqlConnection and SqlCommand. Return FromSqlQuery(context.Database, query, map, parameters, List parameters = null, CommandType commandType = CommandType.Text, ( this DbContext context, string query, Func map, PropertyInfos.SetValue(entity, value, null) Using ( var result = command.ExecuteReader())įor ( int i = 0 i dbDataReaderFields.Any(d =>Įntity.GetType().GetProperty(aField.Name) If ( != ConnectionState.Open)Ĭommand.Transaction = currentTransaction.GetDbTransaction() Ĭommand.CommandTimeout = ( int)commandTimeOutInSeconds Ĭ(parameters.ToArray()) ![]() Using ( var command = database.GetDbConnection().CreateCommand()) (aProp.PropertyType) ? aProp.PropertyType List entityFields = ( from PropertyInfo aProp ( this DatabaseFacade database, string query,Ĭonst BindingFlags flags = BindingFlags.Public |īindingFlags.Instance | BindingFlags.NonPublic Return FromSqlQuery(context.Database, query, parameters, Int? commandTimeOutInSeconds = null) where T : new() ( this DbContext context, string query, List parameters = null, Finally, calling ExecuteNonQuery() to execute the raw SQL query.īool same = mapp.Name = Name & mapp.Type = Type Then, assign all the required parameters to the command object like the SQL, Command Type, SQL parameters, use existing DB transition, and optional command timeout to the command. From the DbContext's database object, create the Db command. The extraction methods are quite similar to the previous one. Var currentTransaction = database.CurrentTransaction Ĭmd.Transaction = currentTransaction.GetDbTransaction() Public static int ExecuteNonQuery( this DatabaseFacade database, Parameters, commandType, commandTimeOutInSeconds) Int value = ExecuteNonQuery(context.Database, command, Public static int ExecuteNonQuery( this DbContext context, string command, Finally, calling ExecuteScalar() to execute the raw SQL query. Then assigning all the required parameters to the command object like the SQL, Command Type, SQL parameters, using existing DB transition, and optional command timeout to the newly created command. From the Ef DbContext's database object, we are accessing the underlying DB connection object and creating the Db command from it. In the extraction methods, we are using ADO.NET features. If ( != ConnectionState.Open)Ĭmd.CommandTimeout = ( int)commandTimeOutInSeconds Ĭmd.Parameters.AddRange(parameters.ToArray()) Using ( var cmd = database.GetDbConnection().CreateCommand()) Public static object ExecuteScalar( this DatabaseFacade database, Object value = ExecuteScalar(context.Database, sql, parameters, Public static object ExecuteScalar( this DbContext context, string sql,ĬommandType commandType = CommandType.Text, Return (source as IInfrastructure).Instance Public static DbTransaction GetDbTransaction( this IDbContextTransaction source) A generic wrapper for builtin FromSqlRawĮxecutes the query, and returns the first column of the first row in the result set returned by the query.Mapp returned data rows to a given type T.Executes Raw SQL queries that do return data.Executes Raw SQL queries that do not return any data. ![]() Optional command type and command timeout.Returns the first column of the first row in the result set returned by the query.In the helper class EfSqlHelper.cs of Database.Core project, we will find the listed extension methods. Here are some extension methods for DbContext and DatabaseFacade objects to run raw SQLs. ToList() Custom DbContext Extension Methods List usersInDb = Db.Users.FromSqlInterpolated ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |