EFCore.Data.Repository
1.0.2
See the version list below for details.
dotnet add package EFCore.Data.Repository --version 1.0.2
NuGet\Install-Package EFCore.Data.Repository -Version 1.0.2
<PackageReference Include="EFCore.Data.Repository" Version="1.0.2" />
paket add EFCore.Data.Repository --version 1.0.2
#r "nuget: EFCore.Data.Repository, 1.0.2"
// Install EFCore.Data.Repository as a Cake Addin #addin nuget:?package=EFCore.Data.Repository&version=1.0.2 // Install EFCore.Data.Repository as a Cake Tool #tool nuget:?package=EFCore.Data.Repository&version=1.0.2
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;
}
}
#####3.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
}
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
- Microsoft.EntityFrameworkCore (>= 5.0.17)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.