无法保存更改:具有存储库模式 + 工作单元的 ApplicationDbContext

Unable to saveChanges: ApplicationDbContext with Repository Pattern + Unit of work

我已经使用工作单元实现了存储库模式,但是我无法 SaveChanges() 并且在敲了敲我的头之后,我发现这是因为我的 DbContext 在工作单元中是不同的class 和通用存储库 Class。它在通用 Repository 方法中成功地将新的 DbSet 添加到 DbContext,但是当它进入 UnitOfWork 的 Commit 方法时,它具有不同的 DbContext,因此之前对 DbContext 的所有更改都消失了。

请告诉我如何创建 ApplicationDbContext 的单个实例,以便它在每个请求中具有相同的 DbContext 实例。 这是代码,

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
                public ApplicationDbContext()
                    : base("DefaultConnection", throwIfV1Schema: false)
                {
                }
       }

    public abstract class GenericRepository<T> : IGenericRepository<T>
          where T : BaseEntity
    {
            protected DbContext _entities;
            protected readonly IDbSet<T> _dbset;

            public GenericRepository(DbContext context)
            {
                _entities = context;
                _dbset = context.Set<T>();
            }
    public virtual T Add(T entity)
            {
                return _dbset.Add(entity);
            }
    }

public sealed class UnitOfWork : IUnitOfWork
    {
private DbContext _dbContext;
public UnitOfWork(DbContext context)
        {

            _dbContext = context;
        }
public int Commit()
        {
            // Save changes with the default options
            return _dbContext.SaveChanges();
        }
public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
 private void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_dbContext != null)
                {
                    _dbContext.Dispose();
                    _dbContext = null;
                }
            }
        }

这是我的服务class,

public abstract class EntityService<T> : IEntityService<T> where T : BaseEntity
    {
        IUnitOfWork _unitOfWork;
        IGenericRepository<T> _repository;

        public EntityService(IUnitOfWork unitOfWork, IGenericRepository<T> repository)
        {
            _unitOfWork = unitOfWork;
            _repository = repository;
        }


        public virtual void Create(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            _repository.Add(entity);
            _unitOfWork.Commit();
        }
    }

这是我的 Unity 解析器 Class,

public static class UnityConfig
    {
        public static void RegisterComponents()
        {
            var container = new UnityContainer();

            // register all your components with the container here
            // it is NOT necessary to register your controllers

            // e.g. container.RegisterType<ITestService, TestService>();
            container.RegisterType<IQuestionService, QuestionService>();
            container.RegisterType<DbContext, ApplicationDbContext>();

            container.RegisterType<IUnitOfWork, UnitOfWork>();
            container.RegisterType<IQuestionRepository, QuestionRepository>();
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
        }
    }

我更改以下行:

container.RegisterType<DbContext, ApplicationDbContext>();

并将其替换为以下内容:

container.RegisterType(typeof(DbContext), typeof(ApplicationDbContext), new PerThreadLifetimeManager());

这是魔术和代码的作品:)