Dot-Net-Core

找不到方法:‘System.Reflection.MethodInfo Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.get_…

  • October 30, 2018

我需要幫助的人 我在一個新項目中,我正在實施身份伺服器 4 並且我正在嘗試使用我在數據庫中的 asp.Identity 恢復以前創建的使用者,以便能夠在我進行外部登錄時進行驗證身份伺服器 4. 對不起我的英語。我的啟動.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()’

我不知道真正的問題是什麼,但是在將 Microsoft.EntityFrameworkCore.Tools 降級到版本 2.1.4 並再次升級到最後一個版本之後問題就消失了。

嗨,你能顯示你的軟體包安裝嗎?當我使用 EFCore 時,我也有同樣的問題。我在安裝包時解決了這個問題

Microsoft.EntityFrameworkCore.Relational >= 2.2.0

引用自:https://stackoverflow.com/questions/53066322