0

I moved all my DBContext call in the repositories.

public class PayAllowanceRepository
{
    private static DBEntities _dbContext = new DBEntities();


    public static void AddAllowance(Allowance payAllowance)
    {
        _dbContext.Allowances.Add(payAllowance);
        _dbContext.SaveChanges();
    }

    public static void AddAllowanceAccumulators(List<Allowance> allowanceList)
    {
        _dbContext = new DBEntities();
        _dbContext.Allowances.AddRange(allowanceList);
        allowanceList.ForEach(p => _dbContext.Entry(p).State = System.Data.Entity.EntityState.Modified);
        _dbContext.SaveChanges();


    }

    // Here
    public static void AddAllowanceAccumulatorsHours(List<Allowance> allowanceListHours)
    {
        _dbContext = new DBEntities();
        _dbContext.Allowances.AddRange(allowanceListHours);
        allowanceListHours.ForEach(x => _dbContext.Entry(x).State = System.Data.Entity.EntityState.Modified);
        _dbContext.SaveChanges();
    }


    public static void UpdateAllowance(Allowance payAllowance)
    {
        _dbContext=new DBEntities();
        _dbContext.Entry(payAllowance).State = System.Data.Entity.EntityState.Modified;
        _dbContext.SaveChanges();
    }


    public static void DeleteAllowance(Guid id)
    {
        var allowance = _dbContext.Allowances.FirstOrDefault(x => x.Id == id);
        _dbContext.Allowances.Remove(allowance);
        _dbContext.SaveChanges();

    }


    public static void UpdateAllowanceRate(Allowance allowanceRate)
    {

        //dbContext.Allowances.Add(allowanceRate);
        _dbContext.Entry(allowanceRate).State = System.Data.Entity.EntityState.Modified;
        _dbContext.SaveChanges();
    }

    public static List<Allowance> GetAllowances(Guid payrollCompanyId)
    {
        var allowance = new List<Allowance>(_dbContext.Allowances.AsNoTracking().Where(x => x.PayrollCompanyId == payrollCompanyId));
        return allowance;
    }

}
  1. Is this the best way to tackle the DBContext in repositories?
  2. It is failing when executing AddAllowanceAccumulatorsHours. I get error message : An entity object cannot be referenced by multiple instances of IEntityChangeTracker. What I am doing wrong?
joinx
  • 21
  • 1
  • 5
  • Possible duplicate of [c# entity framework: correct use of DBContext class inside your repository class](https://stackoverflow.com/questions/33041113/c-sharp-entity-framework-correct-use-of-dbcontext-class-inside-your-repository) – Steve Greene Mar 27 '18 at 20:33

1 Answers1

0

If I do not reinitialse the dbContext, e.g _dbContext=new DBEntities(); I get error message : Attaching an entity of type failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

Getting this issue now in almost all my repositories.

joinx
  • 21
  • 1
  • 5