方法未找到:'System.Reflection.MethodInfo Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.get_SelectAsyncMethod()'。

24

各位,我需要帮助。我正在一个新项目中实现Identity Server 4,尝试从数据库中恢复之前使用asp.Identity创建的用户,以便在使用Identity Server 4进行外部登录时进行验证。抱歉我的英语不好。

这是我的Startup.cs:

public class Startup
{    

public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

 public void ConfigureServices(IServiceCollection services)
{
    #region --Identity ASP

    services.AddDbContext<QGoodDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")),ServiceLifetime.Transient);
    services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<QGoodDbContext>()                   
            .AddDefaultTokenProviders();

    #endregion

    services.AddMvc();

    services.AddScoped<UserManager<ApplicationUser>>();
    services.AddScoped<SignInManager<ApplicationUser>>();

    // configure identity server with in-memory stores, keys, clients and scopes
    services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients());
            //.AddAspNetIdentity<ApplicationUser>();
        }
}

当我初始化数据库时,我已经遇到了这个错误。

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {          

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseIdentityServer();
            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
            InitializeDatabase(app);
        }

        private void InitializeDatabase(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
            {
                //serviceScope.ServiceProvider.GetRequiredService<QGoodDbContext>().Database.Migrate();

                var context = serviceScope.ServiceProvider.GetRequiredService<QGoodDbContext>();

            }
        }

在我的 AccountController 中,当调用 await _userManager.FindByLoginAsync(provider, providerUserId); 时会出现异常。

 public class AccountController : Controller
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly SignInManager<ApplicationUser> _signInManager;
        private readonly QGoodDbContext _context;
        private readonly TestUserStore _users;
        private readonly IIdentityServerInteractionService _interaction;
        private readonly IClientStore _clientStore;
        private readonly IAuthenticationSchemeProvider _schemeProvider;
        private readonly IEventService _events;


        public AccountController(
            UserManager<ApplicationUser> userManager,
            SignInManager<ApplicationUser> signInManager,
            QGoodDbContext context,
            IIdentityServerInteractionService interaction,
            IClientStore clientStore,
            IAuthenticationSchemeProvider schemeProvider,
            IEventService events,
            TestUserStore users = null)
        {
            // if the TestUserStore is not in DI, then we'll just use the global users collection
            // this is where you would plug in your own custom identity management library (e.g. ASP.NET Identity)
            _users = users ?? new TestUserStore(TestUsers.Users);
            _userManager = userManager;
            _signInManager = signInManager;
            _context = context;
            _interaction = interaction;
            _clientStore = clientStore;
            _schemeProvider = schemeProvider;
            _events = events;
        }
          public async Task<IActionResult> ExternalLoginCallback()
        {
            try
            {
                // read external identity from the temporary cookie
                var result = await HttpContext.AuthenticateAsync(IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme);
                if (result?.Succeeded != true)
                {
                    throw new Exception("External authentication error");
                }

                // lookup our user and external provider info
                var (userTest, provider, providerUserId, claims) = FindUserFromExternalProvider(result);
                var user = await _userManager.FindByLoginAsync(provider, providerUserId);
            }
        }
    }

异常信息:

找不到方法:“System.Reflection.MethodInfo Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.get_SelectAsyncMethod()”


感谢您的评论,感激您帮助我。 - Frank Culqui
你实际上是在使用Entity Framework 6还是只是添加了标签? - user4864425
4个回答

33

你能展示一下你安装的包吗?我使用EFCore时也遇到了同样的问题。当我安装这个包后,问题得到了解决。

Microsoft.EntityFrameworkCore.Relational >= 2.2.0

5
降级 EF Core 工具无效,需要使用 Microsoft.EntityFrameworkCore.Relational 库来解决问题!请注意不改变原意,保持简洁。 - Antoine Meltzheim
升级 EF Core 时不要忘记升级数据库提供程序。 - smg
1
刚刚经历了这个问题的痛苦,结果发现我已经点赞了这个答案并收藏了这个问题,哈哈。 - Shelby115
工作得很好。奇怪的是这不是默认包含的。 - Micah Osborne
一个改进建议:Microsoft.EntityFrameworkCore.Relational 必须是 >= 2.2.0 版本。我之前添加了一个低版本,但这并没有解决问题。当我尝试使用 2.2.0 版本时,它立即起作用了。 - Midas
对于大多数与 .NET Core 3.1 和 .NET EFCore 相关的问题,通常是兼容性问题。请为 EFCore 选择正确的版本。 - Say

10

这个问题有几个变体,会表现出MissingMethodExceptionNotImplemementedExceptionMissingFieldException的行为。

看起来这些问题是由于Microsoft.EntityFrameworkCore命名空间内存在冲突的版本引起的。例如,在同一个程序集中引用Microsoft.EntityFrameworkCore.InMemory2.2.0和Microsoft.EntityFrameworkCore.Relational2.1.4会导致此错误。

解决方案是协调nuget包引用的版本,使其不会在内部引用EF Core组件的不同版本。


值得一提的是,一些第三方库如Devart内部也有必须协调的引用。 - Gracie

6

我不知道真正的问题是什么,但在将 Microsoft.EntityFrameworkCore.Tools 降级到版本 2.1.4 并再次升级到最新版本后,问题已经解决。


0

当我在AWS Lambda上实现我的.Net Core 2.2构建项目时,我遇到了这个问题-我做了两件事来解决它:

  1. 从NuGet中包含Microsoft.EntityFrameworkCore.Relational软件包。
  2. 在顶级项目中包含了MySql.Data.EntityFrameworkCore NuGet软件包-由于某种原因,它被包含在上传到AWS的zip文件中,即使它是子项目的依赖项。

我怀疑是第二个操作解决了它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接