How to Use AspNet.Identity core in My Sql database

我们两清 提交于 2019-12-09 04:50:22

问题


I am developing one application using asp dot net core 2 using MySql database.Please help me How can i use Asp Net Identity in MySqlDatabase.


回答1:


I had to do this for a client. And I did in an application with ASP.NET Core 1.0, but for curiosity I also tried for an application in .NET Core 2.0.

What I did was first install the Entity Framework MySQL package from https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql/ using package manager console.

After that I changed in the startup.cs, in the method ConfigureServices, the option UseSqlServer to UseMySql, like the image below.

In my appsettings.json I have the MySQL connection named IdentityConnection like this:

{
    "ConnectionStrings": {
        "IdentityConnection": "Server=127.0.0.1;Database=identitycoredb;Uid=root;Pwd=1234;"
    },

To create the identity tables I executed the migration command in package manager console:

EntityFrameworkCore\Update-Database -Verbose




回答2:


EDIT: At this point, .Net Core 2.0 doesn't support Identity with MySql, in a near future it may be supported again.

__

You need to plug Entity Framework with MySQL with Pomelo's connection and Identity should work. Check this out -> https://damienbod.com/2016/08/26/asp-net-core-1-0-with-mysql-and-entity-framework-core/




回答3:


You Can create an Identity Database along with your MySQL Database and use the Identity database for your authorization

This is how I do it.

   //MySQL Database 
     services.AddDbContext<EFDbContext>(options =>
                options.UseSqlServer("Server = ; Database =MySQL  ; Trusted_Connection = True; MultipleActiveResultSets = true"));
//Identity Database               
     services.AddDbContext<EFIdentityDbContext>(options =>
                    options.UseSqlServer("Server = ; Database = Identity; Trusted_Connection = True; MultipleActiveResultSets = true"));

This should work fine along with your MySQL DB

public class EFIdentityDbContext : IdentityDbContext
{
    public EFIdentityDbContext(DbContextOptions<EFIdentityDbContext> options )
        :base (options)
    {

    }


}


来源:https://stackoverflow.com/questions/46298605/how-to-use-aspnet-identity-core-in-my-sql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!