3

I have some logic that creates a bunch of records based on a start and end dates. When creating these entities they get added to the collection of their parent entity. Needless to say, this can easily explode from a few entities total to a few hundred, easily into the thousands.

My initial logic was jus spinning through my loop logic creating the entities and adding them to the collection of their parents. So my save was one call i.e:

_context.Parent.Add(Parent);
_context.SaveChanges();

in my testing, the Parent entity could have a possible 1400 entities through all its collections (parent, child, grandchild and great-grandchild entities). The save on EF works as expected.

The problem happens when Audit.Net attempts to write the Json for the AuditEvent.ToJson it throws an out of memory exception. this is the offending line entity.AuditData = ev.ToJson(); which takes about 10 min to bubble up to the start where the error is caught. Persistence has already happened in the DB for all the entities that needed to be persisted.

Audit.Core.Configuration.Setup()
            .UseEntityFramework(ef =>
            {
                ef.UseDbContext<LogDbContext>();
                ef.AuditTypeMapper(t => typeof(EntityAuditLog))
                .AuditEntityAction<EntityAuditLog>(
                        (ev, entry, entity) =>
                        {
                            entity.AuditData = ev.ToJson();
                            entity.EntityType = entry.EntityType?.Name;
                            entity.AuditDate = DateTimeOffset.UtcNow;
                            entity.AuditAction = entry.Action;                            
                        })
                .IgnoreMatchedProperties(true);

            });

What I did to get around this was break the logic and _context.SaveChanges() into smaller chunks.

I am curious if there are more configurations in Audit.Net that I missed? Or a strategy when building entities with large collections I should be aware of when using Audit.Net?

ChampChris
  • 1,565
  • 5
  • 26
  • 44
  • 1
    Inserting many elements always takes long. Each Item is one SQL-Command. Try Bulkinsert https://entityframework-extensions.net/bulk-insert – Holger Jan 07 '20 at 15:46
  • I'll give that a look. I was not aware the bulk insert did the entire graph. The persistence from EF isnt the problem though. Its Audit.Net generating the AuditEvent data. Do you know if the bulk insert and Audit.Net work together? – ChampChris Jan 07 '20 at 16:48
  • I don't think so, at least you have to have access to the DbSet.Add or AddRange place. You are not allowed to use it any more. – Holger Jan 07 '20 at 16:56
  • 1
    You are probably serializing not just the new objects but also the parent object containing them including all their child objects. Can you share more details about your model? – Victor Ortuondo Jan 08 '20 at 14:09

0 Answers0