efcore 新特性 SaveChanges Events

efcore 新特性 SaveChanges Events

Intro

昨天早上看到之前關(guān)注的一個 efcore 的 issue 被 closed 捐寥,于是看了一眼, ef core 新合并了一個 PR,在 DbContext 中增加了 SaveChanges 相關(guān)的幾個事件萨蚕,具體的變更可以參數(shù) PR https://github.com/dotnet/efcore/pull/21862

Events

之前寫過兩篇關(guān)于 EF Core 做自動審計的文章酱虎,但是不夠靈活

第一次的實現(xiàn)需要顯示繼承一個 AuditDbContext ,在有些需要沒辦法修改 DbContext 或者原有 DbContext 已經(jīng)有繼承某一個類松蒜,就沒有辦法用了

后面使用 AOP 改進(jìn)了一版扔茅,通過一個審計切面邏輯完整自動審計,但是需要引入 AOP 組件支持秸苗,對于不想引入額外組件的項目來說也并非特別友好

在這個變更之后召娜,我們可以通過 SavingChanges 事件獲取保存之前 DbContext 的狀態(tài),通過 SavedChanges 事件來獲取保存成功的事件惊楼,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;

事件參數(shù)定義如下:

/// <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

除了上面的審計玖瘸,你也可以使用通過這些事件,實現(xiàn)保存之前的自動更新數(shù)據(jù)庫字段的值檀咙,比如 AddUpdate 操作數(shù)據(jù)時自動設(shè)置更新時間等信息

本文提到的特性還未正式發(fā)布雅倒,預(yù)計會在 .net5 下一個預(yù)覽版中發(fā)布,如果想現(xiàn)在要嘗試弧可,請使用 efcore 的 daily build 的包蔑匣,可以參考 https://github.com/dotnet/aspnetcore/blob/master/docs/DailyBuilds.md

Reference

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市侣诺,隨后出現(xiàn)的幾起案子殖演,更是在濱河造成了極大的恐慌,老刑警劉巖年鸳,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件趴久,死亡現(xiàn)場離奇詭異,居然都是意外死亡搔确,警方通過查閱死者的電腦和手機(jī)彼棍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來膳算,“玉大人座硕,你說我怎么就攤上這事√榉洌” “怎么了华匾?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長机隙。 經(jīng)常有香客問我蜘拉,道長萨西,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任旭旭,我火速辦了婚禮谎脯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘持寄。我一直安慰自己源梭,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布稍味。 她就那樣靜靜地躺著废麻,像睡著了一般。 火紅的嫁衣襯著肌膚如雪仲闽。 梳的紋絲不亂的頭發(fā)上脑溢,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天,我揣著相機(jī)與錄音赖欣,去河邊找鬼屑彻。 笑死,一個胖子當(dāng)著我的面吹牛顶吮,可吹牛的內(nèi)容都是我干的社牲。 我是一名探鬼主播,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼悴了,長吁一口氣:“原來是場噩夢啊……” “哼搏恤!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起湃交,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤熟空,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后搞莺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體息罗,經(jīng)...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年才沧,在試婚紗的時候發(fā)現(xiàn)自己被綠了迈喉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,742評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡温圆,死狀恐怖挨摸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情岁歉,我是刑警寧澤得运,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響澈圈,放射性物質(zhì)發(fā)生泄漏彬檀。R本人自食惡果不足惜帆啃,卻給世界環(huán)境...
    茶點故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一瞬女、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧努潘,春花似錦诽偷、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至宝穗,卻和暖如春警绩,著一層夾襖步出監(jiān)牢的瞬間旋膳,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工蜗顽, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人雨让。 一個月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓雇盖,卻偏偏與公主長得像,于是被迫代替她去往敵國和親栖忠。 傳聞我的和親對象是個殘疾皇子崔挖,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,747評論 2 361