实体框架代码优先 4.1 中的自定义非原始类型
我有这个自定义类型:
public struct PasswordString
{
private string value;
public PasswordString(string value)
{
this.value = MD5.CalculateMD5Hash(value);
}
public string Value
{
get { return this.value; }
set { this.value = MD5.CalculateMD5Hash(value); }
}
public static implicit operator PasswordString(string value)
{
return new PasswordString(value);
}
public static implicit operator string(PasswordString value)
{
return value.Value;
}
public static bool operator ==(string x, PasswordString y)
{
return x.CompareTo(y) == 0;
}
public static bool operator !=(string x, PasswordString y)
{
return x.CompareTo(y) != 0;
}
public override string ToString()
{
return Value;
}
}
public static class MD5
{
public static string CalculateMD5Hash(string input)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
}
所以,我想在我的实体框架项目中使用这种类型。如何将类型映射为像字符串一样工作。
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public PasswordString Password { get; set; }
}
使用示例:
User user = new User()
{
Username = "steve",
Password = "apple"
};
System.Console.WriteLine(user.Password == "apple");
System.Console.WriteLine(user.Password);
此代码生成:
True
1F3870BE274F6C49B3E31A0C6728957F
我的目标是对实体框架进行查询以获得如下内容:
var q = from u in users
where u.Username == "steve" && u.Password == "apple"
orderby u.Username
select u;
因此,我永远不需要加密密码,但它将加密存储在数据库中。
我尝试将此类与 EF 一起使用,但没有成功。有没有办法用 Entity Framework 4.1 实现这一点?
I have this custom type:
public struct PasswordString
{
private string value;
public PasswordString(string value)
{
this.value = MD5.CalculateMD5Hash(value);
}
public string Value
{
get { return this.value; }
set { this.value = MD5.CalculateMD5Hash(value); }
}
public static implicit operator PasswordString(string value)
{
return new PasswordString(value);
}
public static implicit operator string(PasswordString value)
{
return value.Value;
}
public static bool operator ==(string x, PasswordString y)
{
return x.CompareTo(y) == 0;
}
public static bool operator !=(string x, PasswordString y)
{
return x.CompareTo(y) != 0;
}
public override string ToString()
{
return Value;
}
}
public static class MD5
{
public static string CalculateMD5Hash(string input)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
}
So, I want tu use this type in my Entity Framework project. How can I map a type to work just like a string.
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public PasswordString Password { get; set; }
}
The using sample:
User user = new User()
{
Username = "steve",
Password = "apple"
};
System.Console.WriteLine(user.Password == "apple");
System.Console.WriteLine(user.Password);
This code produces:
True
1F3870BE274F6C49B3E31A0C6728957F
My goal, is to query against the Entity Framework to have some like this:
var q = from u in users
where u.Username == "steve" && u.Password == "apple"
orderby u.Username
select u;
So then, I never need to encrypt the password, but it will be stored encrypted on the database.
I am trying to use this class with EF, but with no success. There is a way achieve this with Entity Framework 4.1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实体框架不支持类型转换器或任何其他方式将简单数据库列映射到自定义类型,甚至还没有计划在下一个版本中提供此类功能。所以你的问题的答案是:不可能。
Entity framework doesn't support type converters or any other way to map simple database column to your custom type and such feature is even not yet planned for the next release. So the answer to your question is: not possible.
无法在 LINQ to Entities 查询中将类(或结构)用作复杂类型。我为了执行此比较......
必须调用构造函数,该构造函数依次调用
MD5.CalculateMD5Hash
。该方法无法翻译成SQL,查询会抛出异常。 LINQ to Entities 甚至可能不支持任何重载运算符(如==
) - 出于同样的原因:EF 无法将其转换为 SQL。It's not possible to use your class (or struct) as a complex type in a LINQ to Entities query. I order to execute this comparison ...
... the constructor must be called which calls in turn
MD5.CalculateMD5Hash
. This method cannot be translated into SQL and the query will throw an exception. It's likely that LINQ to Entities even doesn't support any overloaded operator (like==
) - for the same reason: EF can't translate it into SQL.