如何不依赖于其内部实现而覆盖EF Core DbContext SaveChanges?

时间:2019-05-21 06:53:24

标签: c# entity-framework entity-framework-core override overloading

我想覆盖我的MyDbContext : DbContext SaveChanges方法 s 。 (所有重载)。如我所见,有四个,但为简单起见,请看两个不是async的重载:

public override int SaveChanges()
{
    MyMethod(); 
    return base.SaveChanges();
}

public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
    MyMethod(); // This will be the 2nd call in case the client code calls the pure SaveChanges()
    return base.SaveChanges(acceptAllChangesOnSuccess);
}

正如预期的那样,在祖先DbContext中实现SaveChanges()会调用另一个SaveChanges(bool acceptAllChangesOnSuccess),但这是当前祖先DbContext的内部实现细节,而我的后代的重载取决于它,我会不想

要对此进行探讨,您必须转到实现源或调试并证明假设。

无论哪种方式,祖先DbContext实现代码都可以在以后更改,这可能会破坏您的覆盖。

概括问题

当覆盖具有多个重载的方法时,如何确保将重载的自定义逻辑应用于任何重载,并且将被调用一次?

0 个答案:

没有答案