Coder Social home page Coder Social logo

dbcontextscopeefcore's Introduction

DbContextScope.EFCore

CI NuGet NuGet version (Zejji.DbContextScope.EFCore)

A library for managing the lifetime of Entity Framework Core DbContext instances.


Note re package versioning: Please use the version of this library which matches your EF Core version.

For EF Core 6, the NuGet package can be found at Zejji.DbContextScope.EFCore6.

To avoid having a separate NuGet package for each EF Core version, for EF Core 7 onwards, the decision was taken to remove the EF Core version from the package name and instead have the Zejji.DbContextScope.EFCore package version number follow the EF Core version - the current NuGet package can be found at Zejji.DbContextScope.EFCore.


This package is based on the original DbContextScope repository by Mehdi El Gueddari with the following changes:

  • updated for .NET 6+ and EF Core (including replacing usages of CallContext with AsyncLocal);
  • added fix for RefreshEntitiesInParentScope method so that it works correctly for entities with composite primary keys;
  • added fix for DbContextCollection's Commit and CommitAsync methods so that SaveChanges can be called more than once if there is a DbUpdateConcurrencyException (see this unmerged pull request in the original DbContextScope repository);
  • added the RegisteredDbContextFactory class as a concrete implementation of the IDbContextFactory interface, which allows users to easily register factory functions for one or more DbContext type(s) during startup; and
  • added unit tests.

Description

Mehdi El Gueddari's original article describing the thinking behind the DbContextScope library can be found here.

In summary, the library addresses the problem that injecting DbContext instances as a scoped dependency (which ordinarily results in one instance per web request) offers insufficient control over the lifetime of DbContext instances in more complex scenarios.

The DbContextScope library allows users to create scopes which control the lifetime of ambient DbContext instances, as well giving control over the exact time at which changes are saved.

For general usage instructions, see article referred to above and the original GitHub repository readme file (a copy of which is included in this repository here). Please note the Mehdime.Entity namespace has been renamed to Zejji.Entity.

The new RegisteredDbContextFactory class can be used as follows:

  • In Startup.cs, register a RegisteredDbContextFactory instance as a singleton and register one or more DbContext factory functions on that instance, e.g.:
using Zejji.Entity;
...
public void ConfigureServices(IServiceCollection services)
{
    ...
    // Create an instance of the RegisteredDbContextFactory
    var dbContextFactory = new RegisteredDbContextFactory();

    // Register factory functions for each of the required DbContext types
    dbContextFactory.RegisterDbContextType<DbContextOne>(() =>
        new DbContextOne(Configuration.GetConnectionString("DatabaseOne")));
    dbContextFactory.RegisterDbContextType<DbContextTwo>(() =>
        new DbContextTwo(Configuration.GetConnectionString("DatabaseTwo")));

    // Register the RegisteredDbContextFactory instance as a singleton
    // with the dependency injection container.
    services.AddSingleton<IDbContextFactory>(dbContextFactory);
    ...
}

See also the unit tests for RegisteredDbContextFactory here.

Getting Started

Dependencies

  • .NET 6+
  • EF Core (version equal to or higher than Zejji.DbContextScope.EFCore package version)

Installing

  • dotnet add package Zejji.DbContextScope.EFCore

License

This project is licensed under the MIT License - see the LICENSE.txt file for details

Acknowledgments

Many thanks to Mehdi El Gueddari for creating the original DbContextScope library.

dbcontextscopeefcore's People

Contributors

zejji avatar dependabot[bot] avatar

Stargazers

 avatar JRT avatar  avatar Steve Py avatar  avatar Ryan Nieuwoudt avatar Anup Singh avatar HiST avatar Jerry Chang avatar

Watchers

 avatar

dbcontextscopeefcore's Issues

Make EF Core 6 build available in NuGet

Would it be possible to update the NuGet package to expose both the EF Core 6 and 7 versions of DbContextScope? I have an existing project stable on .Net 6 and EF Core 6 and went to set up a new dev box but the v6 supporting NuGet package is no longer being offered by NuGet. EF Core 6 is still under support so ideally this package should still be available. I will be upgrading to Core 8 early next year. For now I'll need to try and scavenge a package off an existing development machine.

Edit: Just thought to check and EF7 is compatible with .Net Core 6 so this is a non-disruptive upgrade for this project. Still consider ensuring the NuGet package supports LS support versions for EF.

IOC container registration service of IDbContextScopeFactory

I looked at your README and there is no explanation about registering IDbContextScopeFactory in Startup. Am I doing it correctly?

Singleton or Scoped?

builder.Services.AddSingleton<IDbContextScopeFactory,DbContextScopeFactory>();
builder.Services.AddSingleton<IAmbientDbContextLocator,AmbientDbContextLocator>();

Transaction

How should I implement "beginTransaction" for an insert of 3 different tables (I have to make sure that all 3 are created), the second and the third need the first one to be inserted (PK).

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.