0

I'm currently working on an older MVC 5 website that originally was using a code-first generated database with Entity Framework. It has since been converted to db-first, refactored, and I'm now in the process of removing the old entity c# files and replacing them with ADO.net's entity framework.

The problem I'm currently having is when logging in, I hit the following line in the ApplicationUserManager (which derives from UserManager(TUser, TKey)):

var user = await UserManager.FindByEmailAsync(model.Email);

And I get the following error: "The specified type member 'UserId' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."

Our ApplicationUser entity which mirrors the db table uses the variable 'Id' instead of 'UserId.' I overrode the method in question and it worked but I don't want to do that whenever we need to use the UserManager. So I guess I'm confused, does UserManager require the variable be named, 'UserId?' If not, how can I tell it to use 'Id'?

I don't think I'm missing anything (like accidentally adding a 'UserId' in a LINQ statement somewhere).

McCrockett
  • 159
  • 1
  • 9
  • have you played with the attribute `[Column("your db column name here")]`? If I understood correctly what you need is to map your class property to the proper DB column name, right? – devcrp Jun 17 '20 at 20:34
  • So I'm auto-generating the entity classes using ADO.net entity framework. I don't believe it's recommended to change anything in the class manually. Perhaps I can do it through the edmx somehow? – McCrockett Jun 17 '20 at 20:50

1 Answers1

0

This is what I'm doing and it seems to be working so far. In the Create method of ApplicationUserManager, instead of

var manager = new ApplicationUserManager(new UserStore<ApplicationUser, ApplicationUserRole, int, ApplicationLogin, ApplicationRole, ApplicationClaim>(context.Get<AppDbContext>()));

I created a CustomUserStore that derives from several identity interfaces:

var manager = new ApplicationUserManager(new CustomUserStore(context.Get<AppDbContext>()));

It starts like this:

    public class CustomUserStore : IUserStore<ApplicationUser, int>,
    IUserEmailStore<ApplicationUser, int>,
    IUserRoleStore<ApplicationUser, int>,
    IUserPasswordStore<ApplicationUser, int>,
    IQueryableUserStore<ApplicationUser, int>,
    IUserLockoutStore<ApplicationUser, int>,
    IUserLoginStore<ApplicationUser, int>,
    IDisposable
{
    public IQueryable<ApplicationUser> Users => ctx.ApplicationUsers;

    private AppDbContext ctx { get; set; }

    public CustomUserStore(AppDbContext _ctx)
    {
        ctx = _ctx;
    }

I then override all the necessary methods. It's not what I originally wanted but for now it'll do.

McCrockett
  • 159
  • 1
  • 9