Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddRangeAsync in Disconnected Scenario error #319

Open
Mike6x opened this issue Mar 22, 2023 · 3 comments
Open

AddRangeAsync in Disconnected Scenario error #319

Mike6x opened this issue Mar 22, 2023 · 3 comments

Comments

@Mike6x
Copy link

Mike6x commented Mar 22, 2023

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 without Primary Key ( Id ) => It update OK
  • Update case: update a list with Primary Key ( Id ) => It 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;
        }
    }

  1. 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>();
  1. 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)");
           }
       }
   }
  1. 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
Copy link
Author

Mike6x commented Mar 22, 2023

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.

@fiseni
Copy link
Collaborator

fiseni commented Jul 21, 2023

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
Copy link
Collaborator

fiseni commented Nov 21, 2023

Hey @Mike6x,

Any update here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants