我如何才能登录所有实体的变化,.SaveChanges()首先使用EF代码中? [英] How can I log all entities change, during .SaveChanges() using EF code first?

查看:614
本文介绍了我如何才能登录所有实体的变化,.SaveChanges()首先使用EF代码中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 EF代码首先即可。此外,我正在使用的基础信息库为我所有的库和 IUnitofWork 的注入到库:

I'm using EF code first. Also i'm using a base Repository for all my repositories and an IUnitofWork that inject to the repositories:

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
}   



比如我的 OrderRepository 是这样的:

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

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

和我使用它是这样的:

public void Save(Order order)
{
        using (IUnitOfWork uow = new MyDBContext())
        {
            OrderRepository repository = new OrderRepository(uow); 
            try
            {
               repository.ApplyChanges<Order>(order);    
               uow.SaveChanges();
            }  

        } 
}     



有任何在 .SaveChanges记录所有实体(包括它们的导航属性)的变化史()?我想最初的登录(SAVE发生前)值也发生了变化值(SAVE发生后)。

Is there any way to log change histories of all entities(include their navigation properties) during .SaveChanges()? I want to log original(before save occurs) values also changed values(after save occurs).

推荐答案

您可以得到之前并通过 DbContext.ChangeTracker <打算对所有更改的实体值后, / code>。不幸的是,API是一个有点冗长:

You can get the before and after values for all changed entities by going through DbContext.ChangeTracker. Unfortunately the API is a little verbose:

var changeInfo = context.ChangeTracker.Entries()
            .Where (t => t.State == EntityState.Modified)
            .Select (t => new {
                Original = t.OriginalValues.PropertyNames.ToDictionary (pn => pn, pn => t.OriginalValues[pn]),
                Current = t.CurrentValues.PropertyNames.ToDictionary (pn => pn, pn => t.CurrentValues[pn]),
            });

您可以修改,以包括像实体的类型,如果你需要为你的记录。也有对 DbPropertyValues​​ (该OriginalValues​​和CurrentValues​​类型)一个 ToObject()方法,如果你可以调用已经有办法来记录整个对象,虽然对象从该方法返回不会有填充自己的导航属性。

You can modify that to include things like the type of the entity if you need that for your logging. There is also a ToObject() method on the DbPropertyValues (the type of OriginalValues and CurrentValues) you could call if you already have a way to log whole objects, although the objects returned from that method will not have their navigation properties populated.

您也可以修改代码来获取所有实体通过取出其中,条款,如果这更有意义给您的要求的上下文。

You can also modify that code to get all entities in the context by taking out the Where clause, if that makes more sense given your requirements.

这篇关于我如何才能登录所有实体的变化,.SaveChanges()首先使用EF代码中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆