Method not found: 'System.Reflection.MethodInfo Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.get_SelectAsyncMethod()'

Viewed 5587

People I need help I am in a new project in which I am implementing identity server 4 and I am trying to recover the previously created users with asp.Identity that I have in my database to be able to verify when I make an external login from identity server 4. Sorry my english. My 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>();
        }
}

When I initialize my database I already have this error

 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>();

            }
        }

In my AccountController i have exception when call 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);
            }
        }
    }

Exception is:

Method not found: 'System.Reflection.MethodInfo Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.get_SelectAsyncMethod()'

4 Answers

Hi can you show your packages install ? I have same problem too when i use EFCore. I solved this when I installed package

Microsoft.EntityFrameworkCore.Relational >= 2.2.0

There are several variations of this problem that exhibit the behavior of MissingMethodException, NotImplemementedException or MissingFieldException.

It appears these are caused by conflicting versions within the Microsoft.EntityFrameworkCore namespace. For instance, this error can be caused by referencing Microsoft.EntityFrameworkCore.InMemory 2.2.0 and Microsoft.EntityFrameworkCore.Relational 2.1.4 in the same assembly.

The solution is to harmonize the versions of nuget package references so that they do not internally reference different versions of EF Core components.

I don't know what was the real problem but after downgrade Microsoft.EntityFrameworkCore.Tools to version 2.1.4 and updgrade again to last version problem gone.

I had this problem when I implemented my project on AWS Lambda on an .Net Core 2.2 build- I did two things to fix it:

  1. Include Microsoft.EntityFrameworkCore.Relational package from NuGet.
  2. Included the MySql.Data.EntityFrameworkCore NuGet package in the top level project - for some reason it get included in the zip file that went up to AWS, even though it was a dependency in a sub project.

I suspect it was the second action that fixed it.

Related