简体   繁体   English

C# 实体框架 - 基础 class 计算

[英]C# Entity Framework - base class computation

In our C# code we are using Entity Framework.在我们的 C# 代码中,我们使用的是实体框架。 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?如果有一些选项可以在基本实体 class 本身中编写 function 包装器,在 get 或 set 方法中,在读取和写入期间将在哪里调用此加密解密函数?
  2. Or, can we do something at SQL level.或者,我们可以在 SQL 级别做些什么。

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.我想你已经有了 Encrypt 和 Decrypt 函数,所以我们就这么称呼它们吧。

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.当然你的代码不应该和我的完全一样,但我希望你明白重点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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