Logging every data change with Entity Framework

后端 未结 8 1053
醉酒成梦
醉酒成梦 2020-12-02 14:43

There is a need from a customer to log every data change to a logging table with the actual user who made the modification. The application is using one SQL user to access t

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 15:20

    We had solve this problem in a different way.

    • Inherit a class from your generated entity container class
    • Make the base entity class abstract. You can do it by a partial class definition in a separate file
    • In the inherited class hide the SavingChanges method with your own, using the new keyword in the method definition
    • In your SavingChanges method:

      1. a, open an entity connection
      2. execute the user context stored procedure with ebtityclient
      3. call base.SaveChanges()
      4. close the entityconnection

    In your code you have to use the inherited class then.

提交回复
热议问题