Creating Identity Entities
We will now Proceed on Creating Authentication and Authorization
Which where we will be using Asp.netcore Identity
We need 2 Entities:
- ApplicationUser
- ApplicationRole
The Application User will be used for User Account and doesn't have any connection to our other entities such categories or products. This where Account will handle Authentication
The application Role is basically the Role would be assigned from user. In our case we will assigning role base on their emails.
We will also creating 3 Roles
-Admin
-Seller
-Costumer
Configuration and Fluent Api
Using IEntityTypeConfiguration<>
-We will be using this for extensively configuring database models and relationships.
- ApplicationUserConfiguration: This were the ApplicationUser configured it's properties:
it's properties and relationships
-CategoryConfiguration: This were the Category configured it's properties :
public void Configure(EntityTypeBuilder<Category> builder)
{
builder.ToTable("Categories");
builder.HasKey(c => c.CategoryId);
builder.Property(c => c.Name)
.IsRequired()
.HasMaxLength(100);
builder.Property(c => c.Description)
.HasMaxLength(500);
//relationships to seller and parent category
builder.HasOne(c => c.Seller)
.WithMany(x => x.Categories)
.HasForeignKey(c => c.SellerId)
.OnDelete(DeleteBehavior.SetNull);
//relationship for parent category
builder.HasOne(c => c.ParentCategory)
.WithMany(c => c.SubCategories)
.HasForeignKey(c => c.ParentCategoryId)
.OnDelete(DeleteBehavior.Restrict);
}
And the other configuration follow the same pattern.
Also doing configuration requires understanding about your own Validation. Whether it was fluent validation or Data annotation.
Database Migration and Update Database
We are adding the IdentityDbContext here and we will also apply configuration that we did using ApplyConfigurationsFromAssembly.
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>(options)
{
public virtual DbSet<ApplicationUser> ApplicationUsersDb { get; set; } = null!;
public virtual DbSet<Category> CategoriesDb { get; set; } = null!;
public virtual DbSet<Product> ApplicationRolesDb { get; set; } = null!;
public virtual DbSet<Order> OrderDb { get; set; } = null!;
public virtual DbSet<OrderItem> OrderItemDb { get; set; } = null!;
public virtual DbSet<Review> ReviewDb { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
builder.ApplyConfigurationsFromAssembly(typeof(CategoryConfiguration).Assembly);
builder.ApplyConfigurationsFromAssembly(typeof(ProductConfiguration).Assembly);
builder.ApplyConfigurationsFromAssembly(typeof(OrderItemConfiguration).Assembly);
builder.ApplyConfigurationsFromAssembly(typeof(OrderConfiguration).Assembly);
builder.ApplyConfigurationsFromAssembly(typeof(ReviewConfiguration).Assembly);
}
}
Before we can do Migration
We need to add DbContextFactory:
ApplicationDbContextFactory : IDesignTimeDbContextFactory
Why? In a clean architecture, DbContext lives in the Infrastructure project,
But the EF Core migration tools (dotnet ef migrations add ...) run outside your application—
meaning:
- They cannot access your Program.cs
- They cannot read your DI container
- They do NOT know how to create your DbContext
- They do NOT run the host builder
- They do NOT use your app’s configuration pipeline
After that we will just need to go in User-Secret and add our "ConnectionStrings"
Then Do command: Add-migration and Update-Database
Now The Migration will be added:
After these steps We can now move to Creating Repositories.
What's Next?
In Part 4, I will Implement Authentication and Authorization by doing the Following:
- Creating IAuthService in our Core Application and implement that inside services in our infrastructure
- We will also implement Refresh tokens and Jwt tokens in general -We will assign the role for user base on their email




Top comments (0)