简体   繁体   English

存储库问题中的实体EF6

[英]Entity EF6 in Repository Issues

I moved all my DBContext call in the repositories. 我将所有DBContext调用都移到了存储库中。

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? 这是解决存储库中DBContext的最佳方法吗?
  2. It is failing when executing AddAllowanceAccumulatorsHours. 执行AddAllowanceAccumulatorsHours时失败。 I get error message : An entity object cannot be referenced by multiple instances of IEntityChangeTracker. 我收到错误消息:IEntityChangeTracker的多个实例不能引用一个实体对象。 What I am doing wrong? 我做错了什么?

If I do not reinitialse the dbContext, eg _dbContext=new DBEntities(); 如果我不重新初始化dbContext,例如_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. 现在几乎在我的所有存储库中都遇到了此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM