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

时间:2015-11-22 05:55:55

标签: c# asp.net-mvc entity-framework unit-testing repository-pattern

我已经使用工作单元实现了存储库模式,但是我无法使用SaveChanges()并且在敲击我的脑袋之后,我发现它是因为我的DbContext在工作类的单位和通用存储库类。它在通用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;
                }
            }
        }

这是我的服务类,

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 Resolver 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);
        }
    }

1 个答案:

答案 0 :(得分:1)

我更改了以下行:

container.RegisterType<DbContext, ApplicationDbContext>();

并将其替换为以下内容:

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

这是Magic和Code的作品:)