EFDbFactory.Sqlite
2.1.0
dotnet add package EFDbFactory.Sqlite --version 2.1.0
NuGet\Install-Package EFDbFactory.Sqlite -Version 2.1.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.Sqlite" Version="2.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFDbFactory.Sqlite --version 2.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EFDbFactory.Sqlite, 2.1.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.Sqlite as a Cake Addin #addin nuget:?package=EFDbFactory.Sqlite&version=2.1.0 // Install EFDbFactory.Sqlite as a Cake Tool #tool nuget:?package=EFDbFactory.Sqlite&version=2.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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
Inherit your dbcontext with commondbcontext
public partial class YourDbContext : CommonDbContext
{
public YourDbContext(DbContextOptions<YourDbContext> options)
: base(options)
{
}
}
Dependency Injection
services.AddSingleton<IDbFactory, DbFactory>(provider => new DbFactory(connectionString));
ServiceCollection Extension
Example 1 (No LoggerFactory)
services.AddEfDbFactory(Configuration.GetConnectionString("DbConnection"));
Example 2 (With LoggerFactory)
services.AddEfDbFactory(Configuration.GetConnectionString("DbConnection"), MyLoggerFactory, true);
Example 3 (No LoggerFactory And InMemory Database)
services.AddEfDbFactory("DataSource=:memory:", true));
Example 4 (With LoggerFactory And InMemory Database)
services.AddEfDbFactory("DataSource=:memory:"), MyLoggerFactory, true, 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.CreateTransactional(IsolationLevel.Snapshot);
var context = factory.FactoryFor<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.CreateReadOnly();
var context = factory.FactoryFor<BooksDbContext>();
return context.Book.ToList();
}
Testing
private static IDbFactory GetWritableFactory() => new DbFactory(_connString, true).CreateTransactional().GetAwaiter().GetResult();
[Fact]
public async Task Test_WritableFactory_AutoRollBack()
{
using (var fac = GetWritableFactory())
{
var context = fac.FactoryFor<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 = GetReadonlyFactory())
{
var context = fac2.FactoryFor<TestDbContext>();
Assert.NotEmpty(context.Quiz.ToList());
Assert.InRange(context.Quiz.ToList().Count, 1, 1);
}
}
Product | Versions 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.
-
.NETStandard 2.1
- Microsoft.EntityFrameworkCore (>= 3.1.7)
- Microsoft.EntityFrameworkCore.InMemory (>= 3.1.7)
- Microsoft.EntityFrameworkCore.Sqlite (>= 3.1.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.