我在调用SaveChanges()
后覆盖了base.SaveChanges()
方法来调用存储过程。存储过程存放在我可以轻松模拟的存储库中,但是如何防止DbContext实际将实体保存到数据库?
更新 我还要提一下,我将这两个动作包装在这样的事务中:
public partial class MyDbContext
{
private readonly IProcRepository _procRepository;
public MyDbContext(IProcRepository procRepository, string connectionString) : base(connectionString)
{
_procRepository = procRepository;
}
public override int SaveChanges()
{
var stateEntries = 0;
using (var dbContextTransaction = Database.BeginTransaction())
{
try
{
stateEntries = base.SaveChanges();
_procRepository.MyStoredProcedure();
dbContextTransaction.Commit();
}
catch
{
dbContextTransaction.Rollback();
throw;
}
}
return stateEntries;
}
}