Davood Ramezani
Davood Ramezani

Reputation: 45

How to use identity in asp.net core entity framework core database first approach?

I want to use Microsoft Identity in my project that using entity framework core in database first approach. The project type is asp.net core, mvc(.net core 3.1) help me please.

Upvotes: 3

Views: 1411

Answers (2)

Majid Shahabfar
Majid Shahabfar

Reputation: 4829

Add two separate class library projects into your solution one for IdentityContext and one for ApplicationDbContext.

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

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

Add your entities to ApplicationDBContext class as well as the required ModelBuilder.

Add extension services to each project and add appropriate dbContext service to both. Now you can execute EF Core Migration command separately for each project to create and add tables for User membership and your application main tables. I strongly recommend taking a look at this github resource (Infrastructure.Identity and Infrastructure.Persistence) for more details.

Upvotes: 1

Ali Abbasifard
Ali Abbasifard

Reputation: 428

when you create a project and choose "individual user accounts" in visual studio, Microsoft Identity add automatically to your project as a Razor class library which means, Register and Login UI and functionality, Authentication and Authorization services and... is there from before.
Please tell me what exactly is your problem ?

Upvotes: 0

Related Questions