You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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; }
}
The text was updated successfully, but these errors were encountered:
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.
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:
The text was updated successfully, but these errors were encountered: