Aicrosoft.DataAccess.GenericRepository
6.4.0
dotnet add package Aicrosoft.DataAccess.GenericRepository --version 6.4.0
NuGet\Install-Package Aicrosoft.DataAccess.GenericRepository -Version 6.4.0
<PackageReference Include="Aicrosoft.DataAccess.GenericRepository" Version="6.4.0" />
paket add Aicrosoft.DataAccess.GenericRepository --version 6.4.0
#r "nuget: Aicrosoft.DataAccess.GenericRepository, 6.4.0"
// Install Aicrosoft.DataAccess.GenericRepository as a Cake Addin
#addin nuget:?package=Aicrosoft.DataAccess.GenericRepository&version=6.4.0
// Install Aicrosoft.DataAccess.GenericRepository as a Cake Tool
#tool nuget:?package=Aicrosoft.DataAccess.GenericRepository&version=6.4.0
EFCore.Data.Repository
Repository written in C# for EntityFrameworkCore.
I can guess that you are encountering more than one repository. You can simply use the repository I have prepared in your projects. You can use directly generic repository while injecting in ctor. Since it's already generic, you only need to provide an entity. Let's setup it together.
How to setup?
1. Creating Entity
public class User
{
public Guid Identifier { get; set; }
public string Username { get; set; }
public string PasswordHash { get; set; }
public string Email { get; set; }
}
2.Creating Basic DbContext
public class ApplicationDbContext : DbContext
{
private readonly IConfiguration _configuration;
public ApplicationDbContext(IConfiguration configuration)
{
_configuration = configuration;
}
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// Replace with your connection string.
var connectionString = _configuration.GetConnectionString("TestDbConnection");
options.UseSqlServer(connectionString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>();
}
}
3.Creating Controller and Injecting Repository
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IRepository<User> _repository;
public UserController(IRepository<User> repository)
{
_repository = repository;
}
}
4.Creating Endpoint and Using Repository with CRUD Operation
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IRepository<User> _repository;
public UserController(IRepository<User> repository)
{
_repository = repository;
}
#region Read
[HttpGet]
public async Task<IActionResult> GetAsync()
{
// Get list of data
var data = await _repository.GetListAsync();
return Ok(data);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetAsync(Guid id)
{
// Get data by id
var data = await _repository.GetFirstOrDefaultAsync(x => x.Identifier == id);
return Ok(data);
}
#endregion
#region Create
[HttpPost]
public async Task<IActionResult> PostAsync(User user)
{
// Create
await _repository.AddAsync(user);
return Ok();
}
#endregion
#region Update
[HttpPut("{id}")]
public async Task<IActionResult> PutAsync(User user)
{
// Get entity
var entity = await _repository.GetFirstOrDefaultAsync(x => x.Identifier.Equals(user.Identifier));
// Set new values
entity.Username = user.Username;
entity.Email = user.Email;
entity.PasswordHash = user.PasswordHash;
// Update
await _repository.UpdateAsync(user);
return NoContent();
}
#endregion
#region Delete
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteAsync(Guid id)
{
// Get entity
var entity = await _repository.GetFirstOrDefaultAsync(x => x.Identifier.Equals(id));
// Delete
await _repository.DeleteAsync(entity);
return Ok();
}
#endregion
}
5.DI container Registration
...
services.AddTransient<IRepository<User>, Repository<ApplicationDbContext, User>>();
...
Finally we did it. It will be continued to develop. You can fork. I will make a youtube video.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- Aicrosoft.Extensions (>= 6.4.0)
- Microsoft.EntityFrameworkCore (>= 6.0.24)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Aicrosoft.DataAccess.GenericRepository:
Package | Downloads |
---|---|
NLogAIApp
Extensions of Aicrosoft Ltd. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
6.4.0 | 240 | 11/20/2023 |