2022-08-20 05:47:32 +03:00
|
|
|
using BlueWest.Data;
|
2022-09-17 22:13:35 +03:00
|
|
|
using BlueWest.Data.Application;
|
2022-09-10 07:12:03 +03:00
|
|
|
using BlueWest.WebApi.Context.Users;
|
2022-08-20 05:47:32 +03:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
2022-08-22 00:14:50 +03:00
|
|
|
namespace BlueWest.WebApi.EF.Model
|
2022-08-20 05:47:32 +03:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Database model configuration extensions
|
|
|
|
/// </summary>
|
|
|
|
public static class ModelBuilderExtensions
|
|
|
|
{
|
2022-08-22 00:14:50 +03:00
|
|
|
#region Initialization
|
2022-08-22 02:51:45 +03:00
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
static ModelBuilderExtensions() { }
|
2022-08-22 02:51:45 +03:00
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Setup the database model
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="modelBuilder"></param>
|
|
|
|
public static void ConfigureCurrentDbModel(this ModelBuilder modelBuilder)
|
|
|
|
{
|
|
|
|
modelBuilder
|
|
|
|
.ConfigureDatabaseKeys()
|
2022-08-22 00:14:50 +03:00
|
|
|
.CurrencyModel()
|
2022-09-17 22:13:35 +03:00
|
|
|
.ConfigureUserModel()
|
|
|
|
.ConfigureAppContextModel();
|
|
|
|
|
|
|
|
//.ConfigureIdentityModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Application Users
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Configure App context model
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="modelBuilder"></param>
|
|
|
|
private static void ConfigureAppContextModel(this ModelBuilder builder)
|
|
|
|
{
|
|
|
|
builder.Entity<ApplicationUserRole>().ToTable("UserRoles");
|
|
|
|
builder.Entity<ApplicationUser>(b =>
|
|
|
|
{
|
|
|
|
b.HasMany<ApplicationUserRole>()
|
|
|
|
.WithOne(b => b.User)
|
|
|
|
.HasForeignKey(ur => ur.UserId).IsRequired();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
builder.Entity<ApplicationUser>().ToTable("ApplicationUser")
|
|
|
|
.HasKey(x => x.Id);
|
|
|
|
|
|
|
|
|
|
|
|
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<ApplicationUserRole>().HasOne(x => x.ApplicationRole);
|
|
|
|
builder.Entity<ApplicationRoleClaim>().HasOne<ApplicationRole>(x => x.ApplicationRole);
|
|
|
|
builder.Entity<ApplicationUserClaim>().HasOne<ApplicationUser>(x => x.ApplicationUser);
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
|
|
|
builder.Entity<ApplicationRoleClaim>().ToTable("RoleClaims");
|
|
|
|
builder.Entity<ApplicationUserRole>().ToTable("UserRole");
|
|
|
|
|
|
|
|
|
|
|
|
// Session Token Primary Key
|
|
|
|
builder.Entity<SessionToken>(b =>
|
|
|
|
b.HasKey(x => x.Id));
|
|
|
|
builder.Entity<SessionToken>(b =>
|
|
|
|
b.Property(x => x.Id).ValueGeneratedOnAdd());
|
|
|
|
|
|
|
|
// Session Data
|
|
|
|
builder.Entity<SessionData>()
|
|
|
|
.HasOne(b => b.User)
|
|
|
|
.WithMany(x => x.SessionDatas)
|
|
|
|
.HasForeignKey(x => x.UserId);
|
2022-09-10 07:28:41 +03:00
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
|
|
|
|
// Session Data Primary Key
|
|
|
|
builder.Entity<SessionData>(b =>
|
|
|
|
b.HasKey(x => x.Id));
|
|
|
|
builder.Entity<SessionData>(b =>
|
|
|
|
b.Property(x => x.Id).ValueGeneratedOnAdd());
|
|
|
|
|
|
|
|
|
2022-09-10 07:12:03 +03:00
|
|
|
//.ConfigureIdentityModel();
|
2022-08-20 05:47:32 +03:00
|
|
|
}
|
2022-08-22 00:14:50 +03:00
|
|
|
|
|
|
|
#endregion
|
2022-08-20 05:47:32 +03:00
|
|
|
|
2022-08-22 00:14:50 +03:00
|
|
|
#region DatabasePK
|
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
private static ModelBuilder ConfigureDatabaseKeys(this ModelBuilder modelBuilder)
|
|
|
|
{
|
2022-08-22 00:14:50 +03:00
|
|
|
// 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();
|
2022-08-20 05:47:32 +03:00
|
|
|
|
2022-08-22 00:14:50 +03:00
|
|
|
// 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();
|
2022-08-20 05:47:32 +03:00
|
|
|
|
2022-08-22 00:14:50 +03:00
|
|
|
|
|
|
|
// Product
|
|
|
|
modelBuilder
|
|
|
|
.Entity<Industry>(builder => builder
|
|
|
|
.HasKey(industry => industry.Id));
|
2022-08-20 05:47:32 +03:00
|
|
|
|
2022-08-22 00:14:50 +03:00
|
|
|
modelBuilder
|
|
|
|
.Entity<Industry>()
|
|
|
|
.Property(industry => industry.Id)
|
|
|
|
.ValueGeneratedOnAdd();
|
|
|
|
|
|
|
|
// FinanceOp
|
2022-08-20 05:47:32 +03:00
|
|
|
return
|
|
|
|
modelBuilder.Entity<FinanceOp>(builder => builder
|
|
|
|
.HasKey(financeOp => financeOp.Id));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2022-08-22 00:14:50 +03:00
|
|
|
#endregion
|
2022-08-20 05:47:32 +03:00
|
|
|
|
2022-08-22 00:14:50 +03:00
|
|
|
#region Database Models
|
|
|
|
|
|
|
|
private static ModelBuilder CurrencyModel(this ModelBuilder modelBuilder)
|
2022-08-20 05:47:32 +03:00
|
|
|
{
|
|
|
|
|
2022-09-10 08:05:24 +03:00
|
|
|
modelBuilder.Entity<Currency>().ToTable("Currencies");
|
|
|
|
modelBuilder.Entity<Country>().ToTable("Countries");
|
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
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));
|
2022-09-10 07:28:41 +03:00
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
return modelBuilder;
|
|
|
|
}
|
2022-08-22 00:14:50 +03:00
|
|
|
|
2022-09-06 07:54:48 +03:00
|
|
|
#endregion
|
2022-09-17 22:13:35 +03:00
|
|
|
|
2022-09-06 07:54:48 +03:00
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|