Entity Framework代码优先4.1中的自定义非原始类型 [英] Custom non-primitive type in Entity Framework code-first 4.1

查看:114
本文介绍了Entity Framework代码优先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();
    }
}

因此,我希望tu在我的Entity Framework项目中使用此类型.如何将类型映射为像字符串一样工作.

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; }
}

使用示例:

User user = new User()
{
    Username = "steve",
    Password = "apple"
};

System.Console.WriteLine(user.Password == "apple");
System.Console.WriteLine(user.Password);

此代码产生:

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.

我试图将此类与EF一起使用,但没有成功.有没有一种方法可以通过Entity Framework 4.1实现?

I am trying to use this class with EF, but with no success. There is a way achieve this with Entity Framework 4.1?

推荐答案

实体框架不支持类型转换器或将简单数据库列映射到您的自定义类型的任何其他方式,并且甚至不计划在下一版本中使用该功能. .因此,您的问题的答案是:不可能.

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.

这篇关于Entity Framework代码优先4.1中的自定义非原始类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆