0

I am trying to migrate the authentication models to a MySQL database. I use Pomelo.EntityFrameworkCore.MySql, but when I try to launch the website, I get the following error:

The entity type 'AuthenticationScheme' requires a primary key to be defined

So I try to look up the AuthenticationScheme and find out its and Ilist in the 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; }
    }
}

Then when I click on the AuthenticationScheme, it brings me here:

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

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

But I am not able to edit that nor do I know how to give that Ilist A primary key.

Some more code you might need:

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?}");
            });
        }
    }
}

Now, my question is: how do I solve this error? Or what do I have to do to make it work?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Luuk Wuijster
  • 6,678
  • 8
  • 30
  • 58
  • 1
    Why are you trying to store your view models in the database? This doesn't make much sense. – Kévin Chalet Nov 15 '17 at 17:40
  • I actually really don't know what I am doing. I just want my Authentication models like `users` in a mysql database. – Luuk Wuijster Nov 15 '17 at 18:00
  • You are encountering this error because you are fundamentally taking the wrong approach. Models are persisted to the database. View models are **not**. See [this post](https://stackoverflow.com/questions/4061440/asp-net-mvc-model-vs-viewmodel). – SpruceMoose Nov 16 '17 at 00:30

0 Answers0