【问题标题】:How to Use AspNet.Identity core in My Sql database如何在 Mysql 数据库中使用 Asp Net.Identity 核心
【发布时间】:2018-02-28 03:08:13
【问题描述】:

我正在使用 MySql 数据库使用 asp dot net core 2 开发一个应用程序。请帮助我如何使用 Asp Net Identity MySqlDatabase

【问题讨论】:

标签: mysql asp.net asp.net-core-mvc asp.net-identity-2


【解决方案1】:

我迟到了这个答案,但是,这个开发人员已经很好地把整个解决方案放在了这个存储库中。

https://github.com/jasonsturges/mysql-dotnet-core

把相关的代码块放在这里。

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<IdentityRole>(entity => entity.Property(m => m.Id).HasMaxLength(127));
    builder.Entity<IdentityRole>(entity => entity.Property(m => m.ConcurrencyStamp).HasColumnType("varchar(256)"));

    builder.Entity<IdentityUserLogin<string>>(entity =>
    {
        entity.Property(m => m.LoginProvider).HasMaxLength(127);
        entity.Property(m => m.ProviderKey).HasMaxLength(127);
    });

    builder.Entity<IdentityUserRole<string>>(entity =>
    {
        entity.Property(m => m.UserId).HasMaxLength(127);
        entity.Property(m => m.RoleId).HasMaxLength(127);
    });

    builder.Entity<IdentityUserToken<string>>(entity =>
    {
        entity.Property(m => m.UserId).HasMaxLength(127);
        entity.Property(m => m.LoginProvider).HasMaxLength(127);
        entity.Property(m => m.Name).HasMaxLength(127);
    });
}

这是我在发布此答案时正在从事的项目中测试的。

【讨论】:

  • 基本上,它添加了错误声明的项目,就我的 sql 数据库而言。
【解决方案2】:

我必须为客户做这件事。我在一个使用 ASP.NET Core 1.0 的应用程序中做了,但出于好奇,我还尝试了一个在 .NET Core 2.0 中的应用程序。

我所做的是首先使用包管理器控制台从https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql/ 安装 Entity Framework MySQL 包。

之后我在 startup.cs 中更改了方法 ConfigureServices 中的选项 UseSqlServerUseMySql,如下图。

在我的 appsettings.json 中,我有一个名为 IdentityConnection 的 MySQL 连接,如下所示:

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

为了创建身份表,我在包管理器控制台中执行了迁移命令:

EntityFrameworkCore\Update-Database -Verbose

【讨论】:

  • 这适用于 .Net Core 2.0 和 Pomelo 2.0.1 版本。但是,如果您迁移到 Core 2.1,那么您会看到 pomelo 还没有制作 2.1.0,因此存在一个阻止 db 迁移的错误。
  • 感谢您这么说@MehmetKurtipek。我不知道。
  • @MehmetKurtipek 他们已经修复了最新预发布版本(v2.1.0-rc1-final)中的错误。
  • @AndyFurniss 感谢您提供的信息,我今天注意到了!
  • 在 .Net Core 2.2 中运行,新创建的项目失败,有什么建议吗?
【解决方案3】:

您可以与 MySQL 数据库一起创建身份数据库 并使用身份数据库进行授权

我就是这样做的。

   //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"));

这应该可以与您的 MySQL 数据库一起正常工作

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

    }


}

【讨论】:

    【解决方案4】:

    编辑: 目前.Net Core 2.0 不支持Identity with MySql,近期可能会再次支持。

    __

    您需要使用 Pomelo 的连接将 Entity Framework 与 MySQL 连接,并且 Identity 应该可以工作。看看这个 -> https://damienbod.com/2016/08/26/asp-net-core-1-0-with-mysql-and-entity-framework-core/

    【讨论】:

    • 它将创建所有迁移,即(它将创建所有表,如 AspNetUsers、AspNetRoles 等。)并且此 Lib 是否支持 Core 2.0
    • 你是对的。我用 1.3 做到了,它可以工作。但此时 2.0 还没有支持。很抱歉浪费您的时间。
    • @Bellash 看起来它从 2.0.5 开始就在使用 MySql。检查此博客文章。 retifrav.github.io/blog/2018/03/20/…
    • @Claudio:先生,您能告诉我如何在mysql数据库中使用身份服务器的系统存储过程吗?
    猜你喜欢
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 2010-12-04
    • 2010-11-27
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 2012-12-16
    相关资源
    最近更新 更多