0

I am using the Unit Of Work Repository Pattern, using Unity Dependacy Inhejection to inject the DataContext in the Repo's

I keep getting An entity object cannot be referenced by multiple instances of IEntityChangeTracker. when I use other repo's to look up the object for a foreign Key,

For example my Risk Object has a RiskType, If i set RiskTypeId = 1,object saves if I set RiskTypeId = 1; RiskType = RiskTypeRepo.GetById(1); then i get An entity object cannot be referenced by multiple instances of IEntityChangeTracker

here is me registering the DI

     public static void RegisterComponents()
        {
          UnityContainer container = new Unity.UnityContainer();
          container.EnableDiagnostic();
    
          var context = new EveModel();
    container.RegisterType<IRiskRepository, RiskRepo>(new InjectionConstructor(context));
          container.RegisterType<IRiskService, RiskService>();
        
          //GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver( container);
          GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
        }





//here is the repo constructor and Save method



    public abstract class Repository<TEntity> : IRepositoryBase<TEntity> where TEntity : AbstractBase, 
          new()
        public Repository(EveModel injectedContext)
                {
                    _context = injectedContext;
                    this.currentTime = DateTime.Now;
                        System.Data.Entity.SqlServer.SqlProviderServices.UseScopeIdentity = false;
                }

public async Task<TEntity> SaveAsync(TEntity obj)
        {
 
            try
            {
                //_context.Configuration.AutoDetectChangesEnabled = false;
                TEntity exist = await _context.Set<TEntity>().FindAsync(obj.Id);
                if (exist != null)
                {
                    _context.Entry(exist).CurrentValues.SetValues(obj);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    ///_context.Entry(obj).State = EntityState.Added;
                    _context.Set<TEntity>().Attach(obj);
             


                    await _context.SaveChangesAsync();
                    _context.Entry(obj).Reload();
                    exist = await _context.Set<TEntity>().FindAsync(obj.Id);

                }

                return exist;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

}

China Syndrome
  • 953
  • 12
  • 24

1 Answers1

0

To ensure you share same instance of db context (to avoid this exception), add your db context to container as well.

UnityContainer container = new UnityContainer();
container.EnableDiagnostic();

container.RegisterType<EveModel>();
container.RegisterType<IRiskRepository, RiskRepo>(); // no need in InjectionConstructor parameter as Unity will resolve eveModel instance automatically
Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40