170 lines
5.3 KiB
C#
170 lines
5.3 KiB
C#
using BlueWest.Data;
|
|
using BlueWest.WebApi.Context.Users;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace BlueWest.WebApi.EF.Model
|
|
{
|
|
/// <summary>
|
|
/// Database model configuration extensions
|
|
/// </summary>
|
|
public static class ModelBuilderExtensions
|
|
{
|
|
#region Initialization
|
|
|
|
static ModelBuilderExtensions()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Setup the database model
|
|
/// </summary>
|
|
/// <param name="modelBuilder"></param>
|
|
public static void ConfigureCurrentDbModel(this ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder
|
|
.ConfigureDatabaseKeys()
|
|
.CurrencyModel()
|
|
.ConfigureUserModel();
|
|
|
|
//.ConfigureIdentityModel();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region DatabasePK
|
|
|
|
private static ModelBuilder ConfigureDatabaseKeys(this ModelBuilder modelBuilder)
|
|
{
|
|
// User
|
|
modelBuilder
|
|
.Entity<User>(builder => builder
|
|
.HasKey(user => user.Id))
|
|
.Entity<User>().Property(user => user.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
// Country
|
|
modelBuilder
|
|
.Entity<Country>(builder => builder
|
|
.HasKey(country => country.Id))
|
|
.Entity<Country>().Property(country => country.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
// Currency
|
|
modelBuilder
|
|
.Entity<Currency>(builder => builder
|
|
.HasKey(currency => currency.Id))
|
|
.Entity<Currency>()
|
|
.Property(currency => currency.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
// Company PK
|
|
modelBuilder
|
|
.Entity<Company>(builder => builder
|
|
.HasKey(company => company.Id))
|
|
.Entity<Company>().Property(company => company.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
// Industry PK
|
|
modelBuilder
|
|
.Entity<Industry>(builder => builder
|
|
.HasKey(industry => industry.Id))
|
|
.Entity<Industry>().Property(industry => industry.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
|
|
// Product
|
|
modelBuilder
|
|
.Entity<Industry>(builder => builder
|
|
.HasKey(industry => industry.Id));
|
|
|
|
modelBuilder
|
|
.Entity<Industry>()
|
|
.Property(industry => industry.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
// FinanceOp
|
|
return
|
|
modelBuilder.Entity<FinanceOp>(builder => builder
|
|
.HasKey(financeOp => financeOp.Id));
|
|
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region Database Models
|
|
|
|
private static ModelBuilder CurrencyModel(this ModelBuilder modelBuilder)
|
|
{
|
|
|
|
modelBuilder.Entity<Currency>().ToTable("Currencies");
|
|
modelBuilder.Entity<Country>().ToTable("Countries");
|
|
|
|
modelBuilder
|
|
.Entity<Currency>()
|
|
.HasMany(ub => ub.Countries)
|
|
.WithMany(au => au.Currencies);
|
|
|
|
modelBuilder.Entity<Country>()
|
|
.HasMany(ub => ub.Currencies)
|
|
.WithMany(au => au.Countries);
|
|
|
|
return modelBuilder;
|
|
|
|
}
|
|
|
|
private static ModelBuilder ConfigureUserModel(this ModelBuilder modelBuilder)
|
|
{
|
|
// FinanceOp => User
|
|
modelBuilder
|
|
.Entity<FinanceOp>(builder => builder
|
|
.HasOne<User>()
|
|
.WithMany(ft => ft.FinanceTransactions)
|
|
.HasForeignKey(x => x.UserId));
|
|
|
|
modelBuilder.Entity<User>(b => b.HasOne<ApplicationUser>()
|
|
.WithMany(x => x.Users)
|
|
.HasForeignKey(x => x.ApplicationUserId));
|
|
|
|
return modelBuilder;
|
|
}
|
|
|
|
#endregion
|
|
|
|
public static void ConfigureIdentityModel(this ModelBuilder builder)
|
|
{
|
|
builder.Entity<ApplicationUser>(b =>
|
|
{
|
|
b.HasMany<ApplicationUserRole>().WithOne().HasForeignKey(ur => ur.UserId).IsRequired();
|
|
});
|
|
|
|
builder.Entity<ApplicationRole>(b =>
|
|
{
|
|
b.HasKey(r => r.Id);
|
|
b.HasIndex(r => r.NormalizedName).HasDatabaseName("RoleNameIndex").IsUnique();
|
|
b.ToTable("Roles");
|
|
b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();
|
|
|
|
b.Property(u => u.Name).HasMaxLength(256);
|
|
b.Property(u => u.NormalizedName).HasMaxLength(256);
|
|
|
|
b.HasMany<ApplicationUserRole>().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();
|
|
b.HasMany<ApplicationRoleClaim>().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
|
|
});
|
|
|
|
builder.Entity<ApplicationRoleClaim>(b =>
|
|
{
|
|
b.HasKey(rc => rc.Id);
|
|
b.ToTable("RoleClaims");
|
|
});
|
|
|
|
builder.Entity<ApplicationUserRole>(b =>
|
|
{
|
|
b.HasKey(r => new { r.UserId, r.RoleId });
|
|
b.ToTable("UserRoles");
|
|
});
|
|
}
|
|
|
|
}
|
|
}
|
|
|