3.5 KiB
3.5 KiB
Task 2: Create Database Models
Task Description
Files:
- Create:
src/Fengling.AuthService/Data/ApplicationDbContext.cs - Create:
src/Fengling.AuthService/Models/ApplicationUser.cs - Create:
src/Fengling.AuthService/Models/ApplicationRole.cs - Create:
src/Fengling.AuthService/Data/Migrations/20250201_InitialCreate.cs
Implementation Steps
Step 1: Create ApplicationUser model
Create: src/Fengling.AuthService/Models/ApplicationUser.cs
using Microsoft.AspNetCore.Identity;
namespace Fengling.AuthService.Models;
public class ApplicationUser : IdentityUser<long>
{
public string? RealName { get; set; }
public string? Phone { get; set; }
public long TenantId { get; set; }
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
public DateTime? UpdatedTime { get; set; }
public bool IsDeleted { get; set; }
}
Step 2: Create ApplicationRole model
Create: src/Fengling.AuthService/Models/ApplicationRole.cs
using Microsoft.AspNetCore.Identity;
namespace Fengling.AuthService.Models;
public class ApplicationRole : IdentityRole<long>
{
public string? Description { get; set; }
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
}
Step 3: Create ApplicationDbContext
Create: src/Fengling.AuthService/Data/ApplicationDbContext.cs
using Fengling.AuthService.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace Fengling.AuthService.Data;
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(entity =>
{
entity.Property(e => e.RealName).HasMaxLength(100);
entity.Property(e => e.Phone).HasMaxLength(20);
entity.HasIndex(e => e.TenantId);
entity.HasIndex(e => e.Phone).IsUnique();
});
builder.Entity<ApplicationRole>(entity =>
{
entity.Property(e => e.Description).HasMaxLength(200);
});
}
}
Step 4: Add migration
Run:
cd /Users/movingsam/Fengling.Refactory.Buiding/src/Fengling.AuthService
dotnet ef migrations add InitialCreate -o Data/Migrations
Step 5: Update database
Run:
dotnet ef database update
Step 6: Commit
git add src/Fengling.AuthService/Models/ src/Fengling.AuthService/Data/
git commit -m "feat(auth): add user and role models with EF Core migrations"
Context
This task creates the database models for users and roles with multi-tenant support. We're using ASP.NET Core Identity with long primary keys (IdentityUser) and adding custom properties like RealName, Phone, and TenantId for multi-tenant isolation.
Tech Stack: EF Core 9.0, PostgreSQL, ASP.NET Core Identity
Verification
- ApplicationUser model created with long key type and custom properties
- ApplicationRole model created with description property
- ApplicationDbContext configured with proper entity configurations
- EF Core migration generated successfully
- Database updated with schema
- Committed to git
Notes
- Using long (Int64) as key type for better scalability
- TenantId added to ApplicationUser for multi-tenant support
- Phone number has unique index constraint