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

时间:2018-10-30 14:16:05

标签: .net-core entity-framework-core identityserver4

人们我需要帮助,我正在一个新项目中,我正在其中实施身份服务器4,并且尝试使用asp.Identity恢复以前创建的用户。我在数据库中拥有的身份可以在我进行身份验证时进行验证从身份服务器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()'

4 个答案:

答案 0 :(得分:18)

您好,您可以显示安装的软件包吗?当我使用EFCore时,我也有同样的问题。我在安装软件包

时解决了这个问题
Microsoft.EntityFrameworkCore.Relational

答案 1 :(得分:7)

此问题有多种变体,表现出MissingMethodExceptionNotImplemementedExceptionMissingFieldException的行为。

这些似乎是由Microsoft.EntityFrameworkCore名称空间内的版本冲突引起的。例如,此错误可能是由于在同一程序集中引用Microsoft.EntityFrameworkCore.InMemory 2.2.0和Microsoft.EntityFrameworkCore.Relational 2.1.4引起的。

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

答案 2 :(得分:4)

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

答案 3 :(得分:0)

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

  1. 包括来自NuGet的Microsoft.EntityFrameworkCore.Relational程序包。
  2. 在顶级项目中包含MySql.Data.EntityFrameworkCore NuGet软件包-出于某种原因,即使它是子项目中的依赖项,它也包含在上到AWS的zip文件中。

我怀疑这是修复它的第二步。