gpt4 book ai didi

c# - .Net Core 3.1 使用 MySql 向现有项目添加身份

转载 作者:行者123 更新时间:2023-12-04 00:01:20 24 4
gpt4 key购买 nike

我正在使用带有 MySql 数据库和 Entity Framework Core 的 ASP.NET Core 3.1。我需要为这个项目添加身份。

我已经添加了这个包:

  • Pomelo.EntityFrameworkCore.MySql
  • Microsoft.EntityFrameworkCore
  • Microsoft.AspNetCore.Identity
  • Microsoft.AspNetCore.Identity.UI
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore

我已经创建了一个 ApplicationDbContext:

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

并在ConfigurateServices方法的Startup文件中添加如下代码:

        services.AddDbContext<ApplicationDbContext>(options =>  
options.UseMySql(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();

我还向 appsetings.json 添加了一个 DefaultConnection

所有教程的最后一步是重新创建迁移,这里出现错误。

No migrations configuration type was found in the assembly 'Web'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).

如果我运行“启用迁移”,我会得到

No context type was found in the assembly 'Web'

如何运行迁移和更新数据库?

最佳答案

创建您的用户模型 (ApplicationUser),然后从 IdentityUser 中继承

将您新创建的用户模型作为通用参数添加到 IdentityDbcontext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
}


services.AddIdentity<ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

您的用户模型应该如下所示。

public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string State { get; set; }
public string City { get; set; }
public string Website { get; set; }
public bool IsActive { get; set; }
public string PhotoUrl { get; set; }
}

最后一件事是您创建并运行一个新的迁移来更新您的数据库

注意:如果您的项目中有多个 DbContext,则需要在创建迁移时指定要使用的 DbContext

例如这里我有两个不同的 DbContext

dotnet ef migrations add {migration-name} -c TenantDbContext -s ../AspNetCorePropertyPro.Api/AspNetCorePropertyPro.Api.csproj to run the migration against a client.


dotnet ef database update -c GlobalDbContext -s ../AspNetCorePropertyPro.Api/AspNetCorePropertyPro.Api.csproj to run migration against the global context

dotnet ef migrations add {tenant-migration-name} -o Migrations/Tenants -c TenantDbContext -s ../AspNetCorePropertyPro.Api/AspNetCorePropertyPro.Api.csproj

dotnet ef database update -c GlobalDbContext -s ../AspNetCorePropertyPro.Api/AspNetCorePropertyPro.Api.csproj to run migration against the global context

-o = output directory.

-c = dbcontext to perform the migration if more than one exists.

-s = the path to the startup project.

关于c# - .Net Core 3.1 使用 MySql 向现有项目添加身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60861052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com