Entity Framework Core and column level SQL Server encryption with stored procedures, how to map properties the right way?

huangapple go评论103阅读模式
英文:

Entity Framework Core and column level SQL Server encryption with stored procedures, how to map properties the right way?

问题

我已经使用Entity Framework Core编写了SQL Server加密/解密的存储过程和对称密钥的工作。

我可以将对象保存到数据库中,我将SSNumber参数传递给存储过程,它会加密并存储在SSNumberEncrypted中。

我还编写了一个用于获取记录的存储过程,我设置属性SSNumber = DecryptByKey(SSNumberEncrypted)。如果我在SSMS中运行它,它可以正常工作并获取到SSNumber,但是当我从C#中执行存储过程时,属性值始终为null,因为有NotMapped属性。

是否有一种方法可以实现这个目标?

public class Test 
{
      public long Id { get; set; }
      [NotMapped]
      public string SSNumber { get; set; }
      public byte[] SSNumberEncrypted { get; set; }
}
英文:

I have made work SQL Server encryption/decryption using stored procedures, symmetric keys using Entity Framework Core.

I am able to save the object in the database, I pass the SSNumber parameter to the stored procedure, it encrypts it and stores it in SSNumberEncrypted.

I also coded a stored procedure to get the record, I set the property SSNumber = DecryptByKey(SSNumberEncrypted). If I run it in SSMS, it works and I get the SSNumber, but when I execute the stored procedure from C#, the property value is always null, because of the NotMapped attribute.

Is there a way that I can accomplish this?

public class Test 
{
      public long Id { get; set; }
      [NotMapped]
      public string SSNumber { get; set; }
      public byte[] SSNumberEncrypted { get; set; }
}

答案1

得分: 2

以下是翻译好的内容:

你可以尝试这样做:

public class Test 
{
      public long Id { get; set; }
      [NotMapped]
      public string SSNumber { get=>DecryptByKey(SSNumberEncrypted); 
                              set=>.......; }
      public byte[] SSNumberEncrypted { get; set; }
}
英文:

You may try this:

public class Test 
{
      public long Id { get; set; }
      [NotMapped]
      public string SSNumber { get=>DecryptByKey(SSNumberEncrypted); 
                              set=>.......; }
      public byte[] SSNumberEncrypted { get; set; }
}

huangapple
  • 本文由 发表于 2023年7月20日 13:07:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定