0

I have following method,

public void InsertMembers(List<UserDTO> members)
{
    List<EFModel.ClientData.Member> membersInEfModel = new List<EFModel.ClientData.Member>();
    mapper.Map<List<UserDTO>, List<EFModel.ClientData.Member>>(members, membersInEfModel);

    using (TransactionScope scope = new TransactionScope())
    {
        context.Configuration.AutoDetectChangesEnabled = false;

        foreach (var member in membersInEfModel)
        {

            var existedMember = context.Members.FirstOrDefault(x => x.MemberNumber == member.MemberNumber);
            if (existedMember == null)
            {
                context.Members.Add(member);
            }
            else
            {
                member.MemberId = existedMember.MemberId;
                context.Entry(existedMember).CurrentValues.SetValues(member);
            }
        }
        context.SaveChanges();
        context.Configuration.AutoDetectChangesEnabled = true;
        scope.Complete();
    }
}

which is calling through the bulk of datasets. suppose I have 30000 list, and I called this method 6 times by passing separate 5000 list to the InsertMembers() method.

But after inserting 20000 records, Sometimes I'm getting this error.

The underlying provider failed on Open. at System.Data.Entity.Core.EntityClient.EntityConnection.Open() at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions) at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass7.b__5() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) at System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable.GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source)

Can anyone explain why it happening? this is happening intermittently.

atiyar
  • 7,762
  • 6
  • 34
  • 75
thomsan
  • 433
  • 5
  • 19
  • Does this help? https://stackoverflow.com/questions/2475008/mssql-error-the-underlying-provider-failed-on-open – DavidG Mar 18 '21 at 21:34
  • @DavidG No sir, once I dealing the with huge datasets EF cannot handle it, need to find way to do it – thomsan Mar 19 '21 at 08:49

0 Answers0