protected int GetNumberUpdated(DbContext context) { var resultParameter = new SqlParameter("@result", SqlDbType.Int) { Direction = ParameterDirection.Output }; string sqlQueryCount = SqlQueryBuilder.SelectCountIsUpdateFromOutputTable(this); context.Database.ExecuteSqlRaw($"SET @result = ({sqlQueryCount});", resultParameter); return((int)resultParameter.Value); }
protected async Task <int> GetNumberUpdatedAsync(DbContext context, CancellationToken cancellationToken) { var resultParameters = new List <SqlParameter> { new SqlParameter("@result", SqlDbType.Int) { Direction = ParameterDirection.Output } }; string sqlQueryCount = SqlQueryBuilder.SelectCountIsUpdateFromOutputTable(this); await context.Database.ExecuteSqlRawAsync($"SET @result = ({sqlQueryCount});", resultParameters, cancellationToken).ConfigureAwait(false); // TODO cancellationToken if Not return((int)resultParameters.FirstOrDefault().Value); }
// Compiled queries created manually to avoid EF Memory leak bug when using EF with dynamic SQL: // https://github.com/borisdj/Portal.Data.BulkExtensions/issues/73 // Once the following Issue gets fixed(expected in EF 3.0) this can be replaced with code segment: DirectQuery // https://github.com/aspnet/EntityFrameworkCore/issues/12905 #region CompiledQuery public void LoadOutputData <T>(DbContext context, IList <T> entities) where T : class { bool hasIdentity = OutputPropertyColumnNamesDict.Any(a => a.Value == IdentityColumnName); if (BulkConfig.SetOutputIdentity && hasIdentity) { string sqlQuery = SqlQueryBuilder.SelectFromOutputTable(this); var entitiesWithOutputIdentity = QueryOutputTable <T>(context, sqlQuery).ToList(); UpdateEntitiesIdentity(entities, entitiesWithOutputIdentity); } if (BulkConfig.CalculateStats) { string sqlQueryCount = SqlQueryBuilder.SelectCountIsUpdateFromOutputTable(this); int numberUpdated = GetNumberUpdated(context); BulkConfig.StatsInfo = new StatsInfo { StatsNumberUpdated = numberUpdated, StatsNumberInserted = entities.Count - numberUpdated }; } }