Skip to content

Update Operations

Furkan Güngör edited this page Jun 20, 2022 · 4 revisions

Summary

EasyRepository contains two different methods for update operations.

  • Classes that inherit from the EasyBaseEntity type
  • Custom types

ModificationDate field is automatically set in update operations in classes derived from EasyBaseEntity type.

Update

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public async Task<Author> UpdateAuthorAsync(Guid id)
    {
        var entity = await _unitOfWork.Repository.GetByIdAsync<Author>(asNoTracking: true, id);
        entity.Name = "Franz";
        entity.Surname = "Kafka";
        var result = await _unitOfWork.Repository.UpdateAsync<Author, Guid>(entity);
        await _unitOfWork.Repository.CompleteAsync();
        return entity
    }
}

or

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public async Task<Car> UpdateCarAsync(Guid id)
    {
        var entity = await _unitOfWork.Repository.GetByIdAsync<Car>(asNoTracking: true, id);
        entity.Model = "Astra";
        entity.Brand = "Opel";
        var result = await repository.UpdateAsync(entity);
        await _unitOfWork.Repository.CompleteAsync();
        return entity
    }
}

Multiple Update

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public async Task<List<Author>> UpdateAuthorAsync()
    {
        var entities = await _unitOfWork.Repository.GetMultiple<Author>(asNoTracking: true);
        entities.Foreach(a=>a.Surname= "Kafka");
        var response = await _unitOfWork.Repository.UpdateRangeAsync<Author,Guid>(entities);
        await _unitOfWork.Repository.CompleteAsync();
        return response;
    }
}

or

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public async Task<List<Car>> UpdateCarAsync()
    {
        var entities = await _unitOfWork.Repository.GetMultiple<Car>(asNoTracking: true);
        entities.Foreach(a=>a.Brand = "Mercedes");
        var response = await _unitOfWork.Repository.UpdateRangeAsync<Car>(entities);
        await _unitOfWork.Repository.CompleteAsync();
        return response;
    }
}

Replace

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public async Task<Author> UpdateAuthorAsync(Guid id)
    {
        var entity = await _unitOfWork.Repository.GetByIdAsync<Author>(asNoTracking: true, id);
        entity.Name = "Franz";
        entity.Surname = "Kafka";
        var result = await _unitOfWork.Repository.ReplaceAsync<Author, Guid>(entity);
        await _unitOfWork.Repository.CompleteAsync();
        return entity
    }
}

or

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public async Task<Car> UpdateCarAsync(Guid id)
    {
        var entity = await _unitOfWork.Repository.GetByIdAsync<Car>(asNoTracking: true, id);
        entity.Model = "Astra";
        entity.Brand = "Opel";
        var result = await _unitOfWork.Repository.ReplaceAsync(entity);
        await _unitOfWork.Repository.CompleteAsync();
        return entity
    }
}
Clone this wiki locally