我想知道是否有简单的解决方案,或者我坚持以下:
更新数据库时:
<前>dti.Pass = Crypter.Encrypt(dti.Pass);_db.SubmitChanges();从数据库中选择时:
<前>Data.DbTableItem dti = _db.Single(a=>a.Id == id);dti.Pass = Crypter.Decrypt(dti.Pass);含义 - 我并不是真的喜欢编写重复的代码,这似乎是 LINQ 支持的合乎逻辑的事情;所以我想知道是不是.
您可以添加具有封装此逻辑的属性的分部类,例如:
公共部分类 DbTableItem{公共字符串未加密通行证{得到{返回 Crypter.Decrypt(this.Pass);}放{this.Pass = Crypter.Encrypt(value)}}}希望有帮助:)
I was wondering if there is easy solution to this or I'm stuck with following:
When updating DB:
dti.Pass = Crypter.Encrypt(dti.Pass); _db.SubmitChanges();
When selecting from DB:
Data.DbTableItem dti = _db.Single(a=>a.Id == id); dti.Pass = Crypter.Decrypt(dti.Pass);
Meaning - I am not really into writing repetitive code and this seems like logical thing to be supported by LINQ; so I'm wondering if it is.
You could add a partial class with a property encapsulating this logic like:
public partial class DbTableItem
{
public String UnencryptedPass
{
get
{
return Crypter.Decrypt(this.Pass);
}
set
{
this.Pass = Crypter.Encrypt(value)
}
}
}
Hope it helps : )
这篇关于使用 LINQ 加密列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
为什么我不应该总是在 C# 中使用可空类型Why shouldn#39;t I always use nullable types in C#(为什么我不应该总是在 C# 中使用可空类型)
C# HasValue vs !=nullC# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET:空值和 DbNull —— 有没有更高效的语法C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有没有更高效的语法?)
如何在c#中将空值设置为int?How to set null value to int in c#?(如何在c#中将空值设置为int?)
使用 Min 或 Max 时如何处理 LINQ 中的空值?How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 时如何处理 LINQ 中的空值?)
在 C# 中如果不为 null 的方法调用Method call if not null in C#(在 C# 中如果不为 null 的方法调用)