Coder Social home page Coder Social logo

Comments (2)

ardalis avatar ardalis commented on May 21, 2024

The default EF Core lifetime is Scoped, which has nothing to do with StructureMap (but StructureMap will use this if it's used as the container as in this case). If you want to create separately-scoped instances of DbContext, you can create child scopes as described here:

https://stackoverflow.com/questions/43722463/how-to-create-a-child-scope-from-the-parent-with-default-dependency-injection-in

You might at that point need to pass the dbContext into a new instance of a repo you create yourself, rather than requesting one from the container. Let me know if that helps/makes sense.

from cleanarchitecture.

hugodamiaodias avatar hugodamiaodias commented on May 21, 2024

Sorry it took me so long to get back to this.

I did not manage to solve the issue using your suggestion, but I'll leave here my workaround, in case it can be useful to someone else.

Basically I had a set of events that lead to a loop on 1000+ insertions, and as the dbContextkept everything in memory, it started to get slower and slower...

My solution was to detach all entities from the dbContext after each insertion, as they were not needed anymore.

...
// The event handler that dealt with the 1000+ similar events and thus generated 1000+ inserts
public void HandlePost(MyEntityCreatedEventPost domainEvent)
{
    ...
    _aggregate.Insert(entity);  // note: does a dbContext.SaveChanges()
    _aggregate.DetachAll();
    ...
}

// And the cleanup method to keep the dbContext nimble
public void DetachAll()
{
    foreach (EntityEntry dbEntityEntry in _dbContext.ChangeTracker.Entries().Where(e => e.Entity != null).ToList())
    {
        dbEntityEntry.State = EntityState.Detached;
    }
}

from cleanarchitecture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.