I am using MVC4 DatabaseFirst with EF 5. I get this exception: "The specified type member 'Password' is not supported in LINQ to Entities."
My code:
Auto generated class by EF using DB First approach
// auto-generated-class
public partial class webpages_Users
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public virtual ICollection<webpages_UsersMenus> webpages_UsersMenus { get; set; }
public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
}
Then I created a partial class with a Password property. This will always return blank value and the value will be set from the view.
[MetadataType(typeof(webpages_Users_MD))]
public partial class webpages_Users
{
public string Password { get {return ""; } }
class webpages_Users_MD
{
}
}
In below line I get exception when I am using Password property:
var userList = repository.webpages_Users.Select(x => new { x.UserId, x.UserName, x.Password, x.Email }).ToList(); // Error!!!
However, below works fine without password property
var userList = repository.webpages_Users.Select(x => new { x.UserId, x.UserName, x.Email }).ToList(); // No error here. All ok.
Note: I have to use Select() or something to excude icollection list otherwise jquery used in my view will give error.