Question How to use Entity Framework Extension BulkInsert in Asp.net BoilerPlate

Ckeedee

New member
Joined
Oct 8, 2020
Messages
1
Programming Experience
1-3
I am using Asp.net Zero and i need to use BulkInsert for large data because the normal Insert and Save Changes is very Slow. How do i achieve this in the code below.. See the Repository layer
C#:
private readonly IRepository<InventoryBalance> _repository;

public async Task<InventoryBalance> CreateAsync(InventoryBalance input)
{
    var item = _repository.Insert(input);
    await CurrentUnitOfWork.SaveChangesAsync();
    return item;
}
And this is my Business Logic layer(Application Services) where an calling the Repository Method
C#:
public async Task< PostMessageDto> BulkUpload(List<InventoryBalanceDto> input)
{
    var item = _recEngine.GetList().Where(x=>x.TenantId== _abps.GetTenantId() && x.EntryBy == this.GetUserName() );

    if(item.Count() > 0)
    {
        foreach (var it in item)
        {
            item.ProceedStatus = false;
            await _recEngine.UpdateInvBalanceAsync(it);
        }
    }

    var result = new PostMessageDto { ResponseCode = "00", ResponseDetails = "Successful" };
    var newList = new List<InventoryBalanceDto>();

    newList = input;

    if (input.Count() == 0)
    {
        result.ResponseCode = "99";
        result.ResponseDetails = "No valid record";
    }
    else
    {
        foreach (var Value in input.ToList())
        {
            var acct = new InventoryBalance();

            acct.BankStmtCloseBal = Value.BankStmtCloseBal;
            acct.BankStmtOpenBal = Value.BankStmtOpenBal;
            acct.CashBookCloseBal = Value.CashBookCloseBal;
            acct.CashBookOpenBal = Value.CashBookOpenBal;

            acct.AccountNumber = Value.AccountNumber;
            acct.CompanyCode = this.CompanyCode();
            acct.TenantId = _abps.GetTenantId();
            acct.ProceedStatus = true;
            acct.EndDate = Value.Enddate;
            acct.EntryBy = this.GetUserName();
            acct.EntryDate = DateTime.Now;
            acct.StartDate = Value.StartDate;
            await _recEngine.CreateAsync(acct);
        }
    }

    return result;
}
I tried using this method
C#:
public void CreateBulk(IList<InventoryBalance> inputs)
{
    _repository.GetDbContext().BulkInsert(Inputs);
}
as a method in the Repository Layer but calling the Method in the Bussiness Logic Layer, is giving a compilation error... Is there any better way to do this?
 
Last edited by a moderator:
is giving a compilation error
You have a method named BulkUpload but you're trying to call a method named BulkInsert so that may be the explanation.

If you want to save multiple items then it's probably best not to call SaveChanges after each one. As you should ALWAYS do, think about the logic first and then implement it in code. It should be fairly obvious that you want to something like this:
C#:
_repository.Insert(input);
multiple times before doing this:
C#:
await CurrentUnitOfWork.SaveChangesAsync();
only once.
 
Back
Top Bottom