ardalis / Specification

Base class with tests for adding specifications to a DDD model
MIT License
1.9k stars 242 forks source link

AddRangeAsync in Disconnected Scenario error #319

Open Mike6x opened 1 year ago

Mike6x commented 1 year ago

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

Please help !. My snipt code as following:

  1. Controller
     [HttpPut]
        public async Task<ActionResult<int>> UpdateRangeAsync(UpdateVendorsRequest request)
        {
            return Ok(await Mediator.Send(request));
        }
  1. 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;
        }
    }
4. Repository

public class ApplicationDbRepository : RepositoryBase, IReadRepository, IRepository where T : class, IAggregateRoot { public ApplicationDbRepository(ApplicationDbContext dbContext) : base(dbContext) { } protected override IQueryable ApplySpecification(ISpecification<T, TResult> specification) => specification.Selector is not null ? base.ApplySpecification(specification) : ApplySpecification(specification, false) .ProjectToType();

6. Context

public class ApplicationDbContext : DbContext, IApplicationDbContext { public DbSet Vendors { get; set; } public ApplicationDbContext(DbContextOptions options) : base(options) { } public async Task 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)");
        }
    }
}
7. 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; } }

Mike6x commented 1 year ago

In this github: https://github.com/Mike6x/CQRS_Ardalis_template

I have to controller:

fiseni commented 1 year ago

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.

fiseni commented 9 months ago

Hey @Mike6x,

Any update here?