efcore saveChanges events
efcore 新特性 SaveChanges Events
Intro
昨天早上看到之前關注的一個 efcore 的 issue 被 closed ,于是看了一眼, ef core 新合并了一個 PR,在
DbContext
中增加了
SaveChanges
相關的幾個事件,具體的變更可以參數 PR https://github.com/dotnet/efcore/pull/21862
Events
之前寫過兩篇關于 EF Core 做自動審計的文章
第一次的實作需要顯式繼承一個
AuditDbContext
,在有些需要沒辦法修改
DbContext
或者原有
DbContext
已經有繼承某一個類,就沒有辦法用了,可以參考 https://www.cnblogs.com/weihanli/p/auto-audit-for-entity-framework.html
後面結合 AOP 改進了一版,通過一個審計切面邏輯完成自動審計,但是需要引入 AOP 元件支援,對于不想引入額外元件的項目來說也并非特别友好,可以參考 https://www.cnblogs.com/weihanli/p/12819634.html
在這個 PR 合并之後,我們可以通過
SavingChanges
事件擷取儲存之前
DbContext
的狀态,通過
SavedChanges
事件來擷取儲存成功後的
DbContext
資訊,
SaveChangesFailed
事件擷取儲存失敗資訊
事件定義如下:
/// <summary>
/// An event fired at the beginning of a call to <see cref="M:SaveChanges"/> or <see cref="M:SaveChangesAsync"/>
/// </summary>
public event EventHandler<SavingChangesEventArgs> SavingChanges;
/// <summary>
/// An event fired at the end of a call to <see cref="M:SaveChanges"/> or <see cref="M:SaveChangesAsync"/>
/// </summary>
public event EventHandler<SavedChangesEventArgs> SavedChanges;
/// <summary>
/// An event fired if a call to <see cref="M:SaveChanges"/> or <see cref="M:SaveChangesAsync"/> fails with an exception.
/// </summary>
public event EventHandler<SaveChangesFailedEventArgs> SaveChangesFailed;
事件參數定義如下:
/// <summary>
/// Base event arguments for the <see cref="M:DbContext.SaveChanges" /> and <see cref="M:DbContext.SaveChangesAsync" /> events.
/// </summary>
public abstract class SaveChangesEventArgs : EventArgs
{
/// <summary>
/// Creates a base event arguments instance for <see cref="M:DbContext.SaveChanges" />
/// or <see cref="M:DbContext.SaveChangesAsync" /> events.
/// </summary>
/// <param name="acceptAllChangesOnSuccess"> The value passed to SaveChanges. </param>
protected SaveChangesEventArgs(bool acceptAllChangesOnSuccess)
{
AcceptAllChangesOnSuccess = acceptAllChangesOnSuccess;
}
/// <summary>
/// The value passed to <see cref="M:DbContext.SaveChanges" /> or <see cref="M:DbContext.SaveChangesAsync" />.
/// </summary>
public virtual bool AcceptAllChangesOnSuccess { get; }
}
/// <summary>
/// Event arguments for the <see cref="DbContext.SavingChanges" /> event.
/// </summary>
public class SavingChangesEventArgs : SaveChangesEventArgs
{
/// <summary>
/// Creates event arguments for the <see cref="M:DbContext.SavingChanges" /> event.
/// </summary>
/// <param name="acceptAllChangesOnSuccess"> The value passed to SaveChanges. </param>
public SavingChangesEventArgs(bool acceptAllChangesOnSuccess)
: base(acceptAllChangesOnSuccess)
{
}
}
/// <summary>
/// Event arguments for the <see cref="DbContext.SavedChanges" /> event.
/// </summary>
public class SavedChangesEventArgs : SaveChangesEventArgs
{
/// <summary>
/// Creates a new <see cref="SavedChangesEventArgs" /> instance with the given number of entities saved.
/// </summary>
/// <param name="acceptAllChangesOnSuccess"> The value passed to SaveChanges. </param>
/// <param name="entitiesSavedCount"> The number of entities saved. </param>
public SavedChangesEventArgs(bool acceptAllChangesOnSuccess, int entitiesSavedCount) : base(acceptAllChangesOnSuccess)
{
EntitiesSavedCount = entitiesSavedCount;
}
/// <summary>
/// The number of entities saved.
/// </summary>
public virtual int EntitiesSavedCount { get; }
}
/// <summary>
/// Event arguments for the <see cref="DbContext.SaveChangesFailed" /> event.
/// </summary>
public class SaveChangesFailedEventArgs : SaveChangesEventArgs
{
/// <summary>
/// Creates a new <see cref="SaveChangesFailedEventArgs"/> instance with the exception that was thrown.
/// </summary>
/// <param name="acceptAllChangesOnSuccess"> The value passed to SaveChanges. </param>
/// <param name="exception"> The exception thrown. </param>
public SaveChangesFailedEventArgs(bool acceptAllChangesOnSuccess, [NotNull] Exception exception)
: base(acceptAllChangesOnSuccess)
{
Exception = exception;
}
/// <summary>
/// The exception thrown during<see cref="M:DbContext.SaveChanges"/> or <see cref="M:DbContext.SaveChangesAsync"/>.
/// </summary>
public virtual Exception Exception { get; }
}
More
除了上面的審計,你也可以使用通過這些事件,實作儲存之前的自動更新資料庫字段的值,比如
Add
或
Update
操作資料時自動設定更新時間等資訊
本文提到的特性還未正式釋出,預計會在 .net5 下一個預覽版中釋出,如果想現在要嘗試,請使用 efcore 的 daily build 的包,可以參考 https://github.com/dotnet/aspnetcore/blob/master/docs/DailyBuilds.md
Reference
- https://github.com/dotnet/efcore/issues/15910
- https://github.com/dotnet/efcore/pull/21862
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。