【问题标题】:Entity Framework Core - Audit实体框架核心 - 审计
【发布时间】:2017-04-03 22:35:49
【问题描述】:

有没有办法自动编写审核日志? 我创建了一个DbSet(Of Protocols)

这是我的协议类:

 Public Class Protocol
    Inherits ModelBase
    Implements ILogicalTimestamp

    <Key>
    Public Property ProtocolId As Integer
    <Required>
    Public Property ProtocolText As String
    Public Property CreatedByUserId As Integer?
    <Required>
    Public Property CreatedByUser As User
    <Required>
    <DefaultValue(0)>
    Public Property Type As ProtocolType
    Public Property LinkedToUserId As Integer?
    Public Property LinkedToUser As User
    Public Property LinkedToBusinessPartnerId As Integer?
    Public Property LinkedToBusinessPartner As BusinessPartner
    Public Property LinkedToClientId As Integer?
    Public Property LinkedToClient As Client
    Public Property LinkedToSettingId As Integer?
    Public Property LinkedToSetting As Setting
    Public Property LastUpdateTimestamp As Date? Implements ILogicalTimestamp.LastUpdateTimestamp
    Public Property CreationTimestamp As Date? Implements ILogicalTimestamp.CreationTimestamp

    Public Enum ProtocolType
        AutoChanges = 0
        Explicitly = 1
    End Enum

End Class

如何在协议中编写更改?

我怎样才能将当前的UserId 放入SaveChanges 方法中以填充我的协议类中的CreatedByUserId 属性? 也许任何人都有一个自动执行此操作的想法。

【问题讨论】:

    标签: vb.net entity-framework asp.net-core audit


    【解决方案1】:

    您可以创建一个继承自 DbContext 的自定义 DB Context,然后重写 SaveChanges() 方法,这样您就可以在修改之前/之后操作数据。

    或者您可以使用 Audit.EntityFramework 之类的库,或检查其代码(C# 抱歉)。

    EntityFramework-Plus 库也可能提供帮助。

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 2013-10-31
      • 1970-01-01
      • 2020-05-10
      • 2020-09-12
      相关资源
      最近更新 更多