是否允许从公共覆盖int SaveChanges()中调用MyContext.SaveChanges()? [英] Is it permissible to call MyContext.SaveChanges() from within public override int SaveChanges() ?

查看:78
本文介绍了是否允许从公共覆盖int SaveChanges()中调用MyContext.SaveChanges()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经在 中实施了审计技术public override int SaveChanges(),但由于我有SQLEXPRESS标识列作为我希望审核的实体的主键,我需要执行SaveChanges()来获取真正的密钥,之后我可以准备
审计实体和SaveChanges()再次。在保存审计实体时,再次调用公共覆盖int SaveChanges()。由于审计实体未经审计,因此SaveChanges()会返回调用者。


这是允许的吗?它似乎在开发条件下工作,但不知道我是否会在生产中遇到问题。


这是一个片段: -


public override int SaveChanges()

{


           // Do Stuff来检测变化


            var count = base.SaveChanges();
$


            //然后写下审核<
            if(count> 0)

            {

                DoMyAudit(dbContext,auditedEntitiesList);

                dbContext.SaveChanges();

            }


            //返回原始计数,不包括审核<
           返回计数;

}

解决方案

Jrad,


  ;


我没有看到任何功能问题。


 


谢谢,


亚瑟


Hi,
I have implemented an audit technique within  public override int SaveChanges(), but since I have SQLEXPRESS Identity columns as Primary Keys on the Entities I wish to audit, I need to execute a SaveChanges() to get the real keys, after which I can prepare the Audit entities and SaveChanges() again. That calls public override int SaveChanges() again as the Audit Entities are saved. Since the Audit Entities are not audited, SaveChanges() then returns to the caller.

Is this permissible? It does seem to work in development conditions, but don't know whether I will have problems in production.

Here's a snippet:-

public override int SaveChanges()
{

           //Do Stuff to detect changes

            var count = base.SaveChanges();

            //Then write the Audit
            if (count > 0)
            {
                DoMyAudit(dbContext, auditedEntitiesList);
                dbContext.SaveChanges();
            }

            //Return original count, not including Audit
            return count;
}

解决方案

Jrad,

 

I don’t see any functional issues with this.

 

Thanks,

Arthur


这篇关于是否允许从公共覆盖int SaveChanges()中调用MyContext.SaveChanges()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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