Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom non-primitive type in Entity Framework code-first 4.1
    text
    copied!<p>I have this custom type:</p> <pre><code>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 &lt; hash.Length; i++) { sb.Append(hash[i].ToString("X2")); } return sb.ToString(); } } </code></pre> <p>So, I want tu use this type in my Entity Framework project. How can I map a type to work just like a string.</p> <pre><code>public class User { public int Id { get; set; } public string Username { get; set; } public PasswordString Password { get; set; } } </code></pre> <p>The using sample:</p> <pre><code>User user = new User() { Username = "steve", Password = "apple" }; System.Console.WriteLine(user.Password == "apple"); System.Console.WriteLine(user.Password); </code></pre> <p>This code produces:</p> <pre><code>True 1F3870BE274F6C49B3E31A0C6728957F </code></pre> <p>My goal, is to query against the Entity Framework to have some like this:</p> <pre><code>var q = from u in users where u.Username == "steve" &amp;&amp; u.Password == "apple" orderby u.Username select u; </code></pre> <p>So then, I never need to encrypt the password, but it will be stored encrypted on the database.</p> <p>I am trying to use this class with EF, but with no success. There is a way achieve this with Entity Framework 4.1?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload