相关疑难解决方法(0)

如何在使用EF代码的.SaveChanges()期间记录所有实体更改?

使用EF代码.此外,我正在为我的所有存储库使用基本存储库,并IUnitofWork注入存储库:

public interface IUnitOfWork : IDisposable
{
    IDbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveChanges();
}

public class BaseRepository<T> where T : class
{
    protected readonly DbContext _dbContext;
    protected readonly IDbSet<T> _dbSet;


    public BaseRepository(IUnitOfWork uow)
    {
        _dbContext = (DbContext)uow;
        _dbSet = uow.Set<T>();
    }
    //other methods
}   
Run Code Online (Sandbox Code Playgroud)

例如,我OrderRepository是这样的:

class OrderRepository: BaseRepository<Order>
{
    IUnitOfWork _uow;
    IDbSet<Order> _order;

    public OrderRepository(IUnitOfWork uow)
        : base(uow)
    {
        _uow = uow;
        _order = _uow.Set<Order>();
    }
    //other methods
}
Run Code Online (Sandbox Code Playgroud)

我用这种方式使用它: …

c# logging repository ef-code-first dbcontext

27
推荐指数
3
解决办法
3万
查看次数

标签 统计

c# ×1

dbcontext ×1

ef-code-first ×1

logging ×1

repository ×1