实体类型' AuthenticationScheme'需要一个主键来定义ASP.NET Core 2

时间:2017-11-15 16:48:17

标签: asp.net asp.net-mvc entity-framework asp.net-core

我正在尝试将身份验证模型迁移到MySQL数据库。我使用Pomelo.EntityFrameworkCore.MySql,但是当我尝试启动网站时,出现以下错误:

  

实体类型' AuthenticationScheme'需要定义主键

所以我尝试查找AuthenticationScheme并在Ilist中找到它和ExternalLoginsViewModel

namespace PEV.Models.ManageViewModels
{
    public class ExternalLoginsViewModel
    {
        public IList<UserLoginInfo> CurrentLogins { get; set; }
        public IList<AuthenticationScheme> OtherLogins { get; set; }

        public bool ShowRemoveButton { get; set; }
        public string StatusMessage { get; set; }
    }
}

然后当我点击AuthenticationScheme时,它会把我带到这里:

namespace Microsoft.AspNetCore.Authentication
{
    public class AuthenticationScheme
    {
        public AuthenticationScheme(string name, string displayName, Type handlerType);

        public string DisplayName { get; }
        public Type HandlerType { get; }
    }
}

但我无法编辑,也不知道如何提供Ilist主键。

您可能需要更多代码:

ApplicationDbContext.cs

namespace PEV.Models
{
    public class ApplicationDbContext : DbContext
    {
        /*
         * ==========================================
         * AUTH start
         * ==========================================
         */
        public DbSet<ExternalLoginViewModel> ExternalLogin{ get; set; }
        public DbSet<ForgotPasswordViewModel> ForgotPassword{ get; set; }
        public DbSet<LoginViewModel> Login{ get; set; }
        public DbSet<LoginWith2faViewModel> LoginWith2Fa{ get; set; }
        public DbSet<RegisterViewModel> Register{ get; set; }
        public DbSet<ResetPasswordViewModel> ResetPassword { get; set; }
        public DbSet<ChangePasswordViewModel> ChangePassword { get; set; }
        public DbSet<EnableAuthenticatorViewModel> EnableAuthenticator { get; set; }
        public DbSet<ExternalLoginsViewModel> ExternalLogins { get; set; }
        public DbSet<GenerateRecoveryCodesViewModel> GenerateRecoveryCode { get; set; }
        public DbSet<IndexViewModel> Index { get; set; }
        public DbSet<RemoveLoginViewModel> RemoveLogin { get; set; }
        public DbSet<SetPasswordViewModel> SetPassword { get; set; }
        public DbSet<TwoFactorAuthenticationViewModel> TwoFactorAuthentication { get; set; }
        /*
         * ==========================================
         * AUTH end
         * ==========================================
         */

        public DbSet<Paardenrace> Paardenraces { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Paardenrace>()
                .HasKey(c => new { c.gameid, c.userid });
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseMySql(@"Server=serveraddress.com;database=pev;uid=username;pwd=passwd;");
    }
}

Program.cs的

namespace PEV
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (var context = new ApplicationDbContext())
            {
                // Create database
                context.Database.EnsureCreated();
            }

            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
}

Startup.cs

namespace PEV
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<Models.ApplicationDbContext>();

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<Models.ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddAuthentication().AddGoogle(googleOptions =>
            {
                googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
                googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
            });

            services.AddAuthentication().AddFacebook(facebookOptions =>
            {
                facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
                facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
            });

            // Add application services.
            services.AddTransient<IEmailSender, EmailSender>();

            services.AddMvc();        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

现在,我的问题是:如何解决此错误?或者我需要做些什么来使其发挥作用?

0 个答案:

没有答案