简体   繁体   中英

C# Entity Framework - base class computation

In our C# code we are using Entity Framework. One of column in our table is encrypted and we want to decrypt and encrypt it before data is shown at user interface.

I think, the option is to encrypt or decrypt the string before every call to entity framework, which is very inefficient.

Can someone please suggest:

  1. If there is some option to write a function wrapper in base entity class itself, in get or set methods, where this encryption decryption functions will be called during read and write?
  2. Or, can we do something at SQL level.

Essentially, we do not want to write code before every Entity Framework call and want to do this at some central place.

If I undertand you correctly you have a table (entity) where there is some encrypted column. I suppose you already have Encrypt and Decrypt functions, so let's call them that.

Your code should/could look like this

public class Entity
{
   // this corresponds exactly to column in database
   public string ColumnRawValue
   {
       get;
       private set;
   }

   // this is the abstraction you were asking for
   public string ColumnValue
   {
      get => Decrypt(this.ColumnRawValue);
      set => this.ColumnRawValue = Encrypt(value);
   }
}

Of course your code should not be exactly like mine, but I hope you get the main point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM