I'm using ASP.NET Core 3.1 with a MySql database and Entity Framework Core. I need to add identity to this project.
I've added this packages:
I've created an ApplicationDbContext
:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
And add following code to the Startup
file in the ConfigurateServices
method:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
Also I added a DefaultConnection
to appsetings.json
.
The last step in all tutorials is to recreate Migrations and here I got an error.
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).
And If I run 'Enable-Migrations' I get
No context type was found in the assembly 'Web'
How could I run migrations and update database?
Create your User model (ApplicationUser) then inherent from IdentityUser
add your newly created user model as generate argument to 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();
Your User model should look like this.
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; }
}
The last thing is for you to create and run a new migration to update your database
Note: If you have multiple DbContext in your project you need to specify the DbContext to use when creating the migration
for example here I've two different 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.