2016-11-20 180 views
1

是否有自动写入审计日志的方法吗? 我创建了一个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属性? 也许任何人都有一个想法,自动做到这一点。

回答

1

您可以创建自定义数据库上下文从DbContext继承,然后覆盖的SaveChanges()方法,这样你就可以修改前/后处理数据。

或者你可以使用库像Audit.EntityFramework,或检查其代码(C#抱歉)。

而且图书馆EntityFramework-Plus也许可以帮助。