GenericEntity 0.1.0
See the version list below for details.
dotnet add package GenericEntity --version 0.1.0
NuGet\Install-Package GenericEntity -Version 0.1.0
<PackageReference Include="GenericEntity" Version="0.1.0" />
paket add GenericEntity --version 0.1.0
#r "nuget: GenericEntity, 0.1.0"
// Install GenericEntity as a Cake Addin #addin nuget:?package=GenericEntity&version=0.1.0 // Install GenericEntity as a Cake Tool #tool nuget:?package=GenericEntity&version=0.1.0
Generic Entity
A library to manage EF Core entity easier without adding DbSet<T>
of every single entity into database context
Features
- No need to write each entity field on DbContext
- Support multiple database
- Cleaner query invoke
Setup
First, create interface that represent entity of database, then implement IEntity
<br>
public interface ISampleEntity : IEntity { }
Second, create interface that represent db context that implement IAppDbContext<TEntity>
, where T is interface of entity that created before
public interface ISampleDbContext : IAppDbContext<ISampleEntity> { }
Third, create entity class that impelement interface entity above
public class Blog : ISampleEntity
{
public string Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}
Forth, create db context class that inherit from AppDbContext<TContext>
and implement interface db context that created before, where TContext
is db context itself
public class SampleDbContext : AppDbContext<SampleDbContext>, ISampleDbContext
{
public SampleDbContext() : base() { }
public SampleDbContext(DbContextOptions<SampleDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyEntityRegistration();
}
DbSet<UEntity> IAppDbContext<ISampleEntity>.Entity<UEntity>()
{
return Set<UEntity>();
}
}
Note: You need to override OnModelCreating
and Entity
method like above to make this library work
ApplyEntityRegistration()
will make sure each class that implement interface entity (first step) is migrated into database, and Set<T>
is to return field entity on db context.
Lastly (optional), register interface db context and db context into service container
services.AddScoped<ISampleDbContext, SampleDbContext>();
Usage
Instead of inject db context instance (when using DI), you can inject interface db context (second step) into constructor
public class BlogController : Controller
{
private readonly ISampleDbContext _dbContext;
public BlogController(ISampleDbContext dbContext)
{
_dbContext = dbContext;
}
...
}
And then invoke method Entity<TEntity>
when query to some entity
public class BlogController : Controller
{
...
[HttpGet("{id}")]
public async Task<IActionResult> ReadDetail(string id, CancellationToken ct)
{
var blog = await _dbContext.Entity<Blog>()
.Where(x => x.Id == id)
.FirstOrDefaultAsync(ct);
return new OkObjectResult(blog);
}
}
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.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.EntityFrameworkCore (>= 3.1.16)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.