1

I am currently using EntityFramework.BulkInsert-ef6-ext by hugocl. My software is currently running daily and I am getting "No table mappings provided." once in a while, around 1 to 2 times a week while the rest of the days it runs fine.

Below is the error stack trace:

at EntityFramework.BulkInsert.Helpers.MappedDataReader`1..ctor(IEnumerable`1 enumerable, IEfBulkInsertProvider provider)
   at EntityFramework.BulkInsert.Providers.EfSqlBulkInsertProviderWithMappedDataReader.Run[T](IEnumerable`1 entities, SqlTransaction transaction)
   at EntityFramework.BulkInsert.Providers.ProviderBase`2.Run[T](IEnumerable`1 entities, IDbTransaction transaction)
   at EntityFramework.BulkInsert.Providers.ProviderBase`2.Run[T](IEnumerable`1 entities)
   at EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable`1 entities, BulkInsertOptions options)
   at EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable`1 entities, SqlBulkCopyOptions sqlBulkCopyOptions, Nullable`1 batchSize)
   at EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable`1 entities, Nullable`1 batchSize)
   at ADUtility.Logic.Task.GetDLAssociatesBeforeRunTask.<>c__DisplayClass1_0.<Execute>b__0(TP_DLs dl) in ...

Anyone can provide any ideas on why this is happening?

Below is the code I am using:

 var dlAssociatesBeforeRun = adHelper.GetDLAssociates(dl.DLName)
                                .Select(x =>
                                    new TP_DLAssociatesBeforeRun
                                    {
                                        DLID = dl.ID,
                                        ADUsername = x.ADUsername,
                                        CreateDate = DateTime.Now,
                                        CreateBy = "ADUtility.CLI",
                                        UpdateDate = DateTime.Now,
                                        UpdateBy = "ADUtility.CLI"
                                    }).ToList();
                            _logger.Trace($"Detected {dlAssociatesBeforeRun.Count} for DL {dl.DLName}");
                            // ReSharper disable once InvertIf
                            if (dlAssociatesBeforeRun.Any())
                            {
                                db.BulkInsert(dlAssociatesBeforeRun);
                                db.SaveChanges();

Below is the GetDLAssociates method which is basically just returns a list of view model objects:

public List<DLAssociateViewModel> GetDLAssociates(string groupName, PrincipalContext context = null)
    {
        var dlAssociate = new List<DLAssociateViewModel>();
        var group = GroupPrincipal.FindByIdentity(context ?? GlobalContext, groupName);
        if (group == null)
        {
            throw new Exception("DL not found on Active Directory");
        }
        dlAssociate.AddRange(group.Members.OfType<UserPrincipal>()
            .Select(member => new DLAssociateViewModel
            {

                DLName = groupName,
                ADUsername = string.IsNullOrEmpty(member.EmployeeId) ? member.SamAccountName : member.EmployeeId,
            }));
        return dlAssociate;
    }
vylor
  • 11
  • 4

1 Answers1

0

I'm faced with exactly the same issue. I'm pretty sure it is related to some static properties and it occurs when I use more than one EF context (I'm using Parallel.ForEach and create dedicated EF context for every thread).

I played with ParallelOptions and found when MaxDegreeOfParallelism==1 the issue is never reproduced.

So, quick workaround for me is: add Context.BulkInsert(new SomeEntity[0]) (TP_DLAssociatesBeforeRun instead of SomeEntity in your case) in my repository constructor. It is like 'warming up' and will initialize MappedDataReader (used in EntityFramework BulkInsert-ef6-ext extension) properly.

Btw, you don't need db.SaveChanges(); after BulkInsert since it executed directly on DB.