保存更改在Encapsulated UnitOFWork类中不起作用

时间:2013-07-18 19:07:45

标签: c# entity-framework

您好我正在尝试首先使用实体​​框架代码创建一个通用存储库,并将所有内容封装在UnitOfWork中但是必须有错误,因为当我尝试添加它并使用我的封装的SaveChanges时它不起作用。 这是我的存储库代码:

public class Repository<T> : IRepository<T> where T : class
{
    private DbContext Context { get; set; }
    private DbSet<T> DbSet
    {
        get { return Context.Set<T>(); }
    }

    public Repository(DbContext context)
    {
        Context = context;
    } 

    public virtual IEnumerable<T> GetAll()
    {
        return DbSet;
    }

    public virtual T GetById(int id)
    {
        return DbSet.Find(id);
    }

    public virtual void Add(T entity)
    {
        DbEntityEntry dbEntityEntry = Context.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            dbEntityEntry.State = EntityState.Added;
        }
        else
        {

            DbSet.Add(entity);
        }
    }

    public virtual void Update(T entity)
    {
        DbEntityEntry dbEntityEntry = Context.Entry(entity);
        if (dbEntityEntry.State == EntityState.Detached)
        {
            DbSet.Attach(entity);
        }
        DbSet.Attach(entity);
    }

    public virtual void Remove(T entity)
    {
        DbEntityEntry dbEntityEntry = Context.Entry(entity);
        if (dbEntityEntry.State != EntityState.Deleted)
        {
            dbEntityEntry.State = EntityState.Deleted;
        }
        else
        {
            DbSet.Attach(entity);
            DbSet.Remove(entity);   
        }
    }

    public virtual void Remove(int id)
    {
        var entity = GetById(id);
        if (entity == null)
        {
            return;
        }
        Remove(entity);
    }
}

这是我的UnitOfWork代码:

public class UnitOfWork
{
    private readonly RepositoryFactory repositoryFactory;

    private DatabaseContext DbContext
    {
        get { return new DatabaseContext(); }
    }

    public IRepository<Product> Products
    {
        get
        {
            return repositoryFactory.GetRepository<Product>(DbContext);
        }
    }

    public UnitOfWork()
    {
        repositoryFactory = new RepositoryFactory();
    }

    public void SavaChanges()
    {
        DbContext.SaveChanges();
    }
}

这是我调用添加数据和获取数据的代码:

var sa = new UnitOfWork();
var repository = sa.Products;;
var result = repository.GetAll();
var resultbyId = repository.GetById(3);

 var product = new Product()
 {
     Name = "sddasd",
     CategoryId = 1,
     SubcategoryId = 1,
     Price = 21,
     Description = "dsadasfas",
     ImagePath = "Dsadas",
     NumberOfProducts = 29
  };
   repository.Add(product);
   sa.SavaChanges()

运行此代码之后,似乎由于某种原因,在我的UnitOfWork类中封装的SaveChanges不起作用。

但是,例如,我会在DbSet.Add(实体)

之后添加此行
Context.SaveChanges() 

似乎将对象添加到数据库中。

如何使UnitOfWork SaveChanges方法起作用?

1 个答案:

答案 0 :(得分:3)

您的代码中存在问题:

private DatabaseContext DbContext
{
    get { return new DatabaseContext(); }
}

您实际执行的操作是每次访问您的媒体资源时创建新的上下文。当您的Repository<T>正确保存一个上下文并重新使用相同的上下文时,当您致电UnitOfWork.SaveChanges时,您将保存在新创建的上下文中,而不会进行任何更改。

本着UnitOfWork的精神,您希望您的上下文生活在封闭类(UnitOfWork)的整个生命周期中。试试这个:

private DatabaseContext dbContext;
private DatabaseContext DbContext
{
    get { return dbContext ?? (dbContext = new DatabaseContext()); }
}

这样,您DatabaseContext只会在第一次访问UnitOfWork媒体资源时在DbContext的生命周期内创建一次。