Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 799
Frank Culqui Avatar asked Oct 30 '18 14:10

Frank Culqui


3 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
like image 92
Anton Makieiev Avatar answered Nov 15 '22 07:11

Anton Makieiev


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.

like image 9
Gracie Avatar answered Nov 15 '22 06:11

Gracie


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.

like image 6
Ali Yousefi Avatar answered Nov 15 '22 06:11

Ali Yousefi