Skip to content

Update Operations

Furkan Güngör edited this page Nov 30, 2021 · 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 IRepository repository;
    public MyClass(IRepository repository)
    {
        this.repository = repository;
    }
    public async Task<Author> UpdateAuthorAsync(Guid id)
    {
        var entity = await repository.GetByIdAsync<Author>(asNoTracking: true, id);
        entity.Name = "Franz";
        entity.Surname = "Kafka";
        var result = await repository.UpdateAsync<Author, Guid>(entity);
        return entity
    }
}

or

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

Multiple Update

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

or

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

Replace

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

or

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