Base solution for your next web application
Open Closed

Overide db save based on Data Attribute #4493


User avatar
0
rattlehub created

Is it possible to overide the save on the database context. We have a custon attribute that we need to check if the value has the attribute defined and then modify the data. Could you advise me the best way to do this.

We are basically looking to implememnt something like this where we can see if a Encryped attribute is set and then if need be encrypt the data

<a class="postlink" href="https://gist.github.com/albertbori/e95860644e69c1572441">https://gist.github.com/albertbori/e95860644e69c1572441</a>


1 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Yes, you can override SaveChanges().

    Are you using EF Core? If yes, then the main difference is:

    var pendingEntities = ChangeTracker.Entries()
        .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified)
        .ToList();
    

    You can try that and let us know what errors you get.