EFDbFactory.Sql 2.3.0

dotnet add package EFDbFactory.Sql --version 2.3.0                
NuGet\Install-Package EFDbFactory.Sql -Version 2.3.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EFDbFactory.Sql" Version="2.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFDbFactory.Sql --version 2.3.0                
#r "nuget: EFDbFactory.Sql, 2.3.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EFDbFactory.Sql as a Cake Addin
#addin nuget:?package=EFDbFactory.Sql&version=2.3.0

// Install EFDbFactory.Sql as a Cake Tool
#tool nuget:?package=EFDbFactory.Sql&version=2.3.0                

Entity Framework Core Factory Pattern for Sql Server

Factory Pattern for Entity Framework Core. It helps for multiple EF DbContext with this pattern. You can create readonly context and read-write with transaction.

How to use it

Important Inherit your dbcontext with EFDbContext

public partial class YourDbContext : EFDbContext
    {
        public YourDbContext(DbContextOptions<QuizDbContext> options)
            : base(options)
        {
        }
    }

Dependency Injection

services.AddSingleton<IDbFactory, DbFactory>(provider => new DbFactory(connectionString));

ServiceCollection Extension

Example 1 (No LoggerFactory)
	services.AddEfDbFactory(connectionString: Configuration.GetConnectionString("DbConnection"));

Example 2 (With LoggerFactory)
	services.AddEfDbFactory(connectionString: Configuration.GetConnectionString("DbConnection"), loggerFactory: MyLoggerFactory, enableSensitiveDataLogging: true);

Example 3 (With Options)
            services.AddEfDbFactory(new EfDbFactoryOptions
                                        {
                                            ConnectionString = Configuration.GetConnectionString("DbConnection"),
                                            LoggerFactory =  MyLoggerFactory,
                                            EnableSensitiveDataLogging = true
                                        });

Injection in your controller

private readonly IDbFactory _factoryConn;

public WriteController(IDbFactory factoryConn)
{
  _factoryConn = factoryConn ?? throw new ArgumentNullException(nameof(factoryConn));
}

ReadWrite Factory

public async Task CreateBook(int authorId, string title)
        {
            using var factory = await factoryConn.Create(IsolationLevel.Snapshot);
            var context = factory.For<BooksDbContext>();

            var book = new Book
            {
                Title = "New book",
                AuthorId = authorId
            };
            context.Book.Add(book);
            await context.SaveChangesAsync();
            factory.CommitTransaction();
        }

Readonly factory

public async Task<IEnumerable<Book>> GetAllBooks()
        {
            using var factory = await factoryConn.Create();
            var context = factory.For<BooksDbContext>();
            return context.Book.ToList();
        }

Testing

private const string _connString =
            "Server=localhost\\sqlexpress;Database=QuizDb;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=True;ConnectRetryCount=0";

private static async Task<IDbFactory> GetNoCommitFactoryAsync() => await new DbFactory(_connString).CreateNoCommit();

[Fact]
public async Task Test_NoCommitFactory_AutoRollBack()
{
    using (var fac = await GetNoCommitFactory())
    {
        var context = fac.For<TestDbContext>();

        var quiz = new Quiz() {Title = "Test 1"};
        context.Quiz.Add(quiz);
        await context.SaveChangesAsync();

        var q = Assert.Single(context.Quiz.ToList());
        Assert.NotNull(q);
        Assert.Equal("Test 1", q.Title);
    }

    using (var fac2 = await GetNoCommitFactory())
    {
        var context = fac2.For<TestDbContext>();
        Assert.Empty(context.Quiz.ToList());
    }
}

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.3.0 452 1/7/2021
2.2.0 452 9/11/2020
2.1.1 439 8/31/2020
2.1.0 468 7/14/2020
2.0.0 482 11/19/2019
1.0.5 602 11/7/2019
1.0.4 577 11/3/2019
1.0.3 590 10/30/2019