0

我目前正在开发一个较旧的 MVC 5 网站,该网站最初使用带有实体框架的代码优先生成的数据库。它已被转换为 db-first,重构,我现在正在删除旧的实体 c# 文件并用 ADO.net 的实体框架替换它们。

我目前遇到的问题是在登录时,我在 ApplicationUserManager 中点击了以下行(它派生自 UserManager(TUser, TKey)):

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

我收到以下错误:“ LINQ to Entities 不支持指定的类型成员 'UserId'。仅支持初始化程序、实体成员和实体导航属性。

镜像 db 表的 ApplicationUser 实体使用变量“Id”而不是“UserId”。我覆盖了有问题的方法并且它有效,但是每当我们需要使用 UserManager 时我不想这样做。所以我想我很困惑,UserManager 是否需要将变量命名为“UserId”?如果没有,我怎么能告诉它使用'Id'?

我认为我没有遗漏任何东西(比如在某处的 LINQ 语句中意外添加了“UserId”)。

4

1 回答 1

0

这就是我正在做的事情,到目前为止它似乎正在工作。在 ApplicationUserManager 的 Create 方法中,而不是

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

我创建了一个派生自多个身份接口的 CustomUserStore:

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

它是这样开始的:

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

然后我覆盖所有必要的方法。这不是我最初想要的,但现在它会做。

于 2020-06-18T18:52:54.167 回答