Specification
Specification copied to clipboard
AddRangeAsync in Disconnected Scenario error
I am using Ardalis 6.1.0 + MediatR in NET 6 CQRS Pattern and try new AddRangeAsync function. Everthing seamly is OK but in Disconnected Scenario, I have error
- Insert case: when update a list <T> without Primary Key ( Id ) => It update OK
- Update case: update a list <T> with Primary Key ( Id ) => It error.
Please help !. My snipt code as following:
- Controller
[HttpPut]
public async Task<ActionResult<int>> UpdateRangeAsync(UpdateVendorsRequest request)
{
return Ok(await Mediator.Send(request));
}
- Request
public class UpdateVendorsRequest : IRequest<int>
{
public List<Vendor> Vendors { get; set; }
}
public class UpdateVendorsRequestHandler : IRequestHandler<UpdateVendorsRequest, int>
{
private readonly IRepository<Vendor> _repository;
public UpdateVendorsRequestHandler(IRepository<Vendor> repository, IStringLocalizer<UpdateVendorsRequestHandler> localizer) =>
(_repository, _t) = (repository, localizer);
public async Task<int> Handle(UpdateVendorsRequest request, CancellationToken cancellationToken)
{
await _repository.AddRangeAsync(request.Vendors, cancellationToken);
return request.Vendors.Count;
}
}
- Repository
public class ApplicationDbRepository<T> : RepositoryBase<T>, IReadRepository<T>, IRepository<T>
where T : class, IAggregateRoot
{
public ApplicationDbRepository(ApplicationDbContext dbContext)
: base(dbContext)
{
}
protected override IQueryable<TResult> ApplySpecification<TResult>(ISpecification<T, TResult> specification) =>
specification.Selector is not null
? base.ApplySpecification(specification)
: ApplySpecification(specification, false)
.ProjectToType<TResult>();
- Context
public class ApplicationDbContext : DbContext, IApplicationDbContext
{
public DbSet<Vendor> Vendors { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{ }
public async Task<int> SaveChanges()
{
return await base.SaveChangesAsync();
}
protected override void OnModelCreating(ModelBuilder builder)
{
foreach (var property in builder.Model.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?)))
{
property.SetColumnType("decimal(18,2)");
}
}
}
- Entity
public class Vendor : BaseEntity, IAggregateRoot
{
public string Code { get; set; }
public string Name { get; set; }
public string? Description { get; set; }
}
public abstract class BaseEntity
{
public int Id { get; set; }
}
In this github: https://github.com/Mike6x/CQRS_Ardalis_template
I have to controller:
- ProductController using EF UpdateRange. The methord UpdateRange work well.
- VenderController using Ardalist UpdateRange, the methord UpdateRange failed as mention aboved.
Ok, it seems you're using AddRangeAsync
for both adding and updating records, right?
Did you try without using specs and repositories, and working directly with dbContext instead? It seems it's not about this library per se, but that's how the EF tracker works.
Hey @Mike6x,
Any update here?