Upgrade EF models and add some EF mappings

This commit is contained in:
Jiří Vrabec 2025-08-09 12:54:29 +02:00
parent b7996872cb
commit 2830d5dd48
167 changed files with 5950 additions and 59 deletions

View file

@ -1,10 +1,71 @@
using DrinkRateAPI.DbEntities;
using Microsoft.EntityFrameworkCore;
namespace DrinkRateAPI.Contexts;
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<DbContext> options) :
base(options)
{ }
public DbSet<DbCompany> Companies { get; set; }
public DbSet<DbCompanyRating> CompanyRatings { get; set; }
public DbSet<DbCompanyTable> CompanyTables { get; set; }
public DbSet<DbCompanyTableView> CompanyTableViews { get; set; }
public DbSet<DbProduct> Product { get; set; }
public DbSet<DbProductRating> ProductRating { get; set; }
public DbSet<DbProductTable> ProductTable { get; set; }
public DbSet<DbProductTableView> ProductTableView { get; set; }
public DbSet<DbUser> Users { get; set; }
public DbSet<DbUserCompanyTableStat> UserCompanyTableStats { get; set; }
public DbSet<DbUserProductTableStat> UserProductTableStats { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
optionsBuilder.UseNpgsql(configuration.GetConnectionString("Local"));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Company
modelBuilder.Entity<DbCompany>(c =>
{
c.HasKey(c => c.Id);
c.HasMany(c => c.Products)
.WithOne(p => p.Company)
.HasForeignKey(p => p.CompanyId);
c.HasMany(c => c.CompanyRatings)
.WithOne(cr => cr.Company)
.HasForeignKey(cr => cr.CompanyId);
c.Property(c => c.CompanyName)
.IsRequired()
.HasMaxLength(50);
c.HasIndex(c => c.CompanyName)
.IsUnique();
// to do: index by Sum / Count
});
// Company Rating
modelBuilder.Entity<DbCompanyRating>(cr =>
{
cr.HasKey(cr => new { cr.UserId, cr.CompanyId });
cr.HasOne(cr => cr.User)
.WithMany(u => u.CompanyRatings);
cr.HasOne(cr => cr.Company)
.WithMany(c => c.CompanyRatings);
});
// to do: othe objects
}
}

View file

@ -4,11 +4,16 @@ public class DbCompany
{
public Guid Id { get; set; }
public Guid TableId { get; set; }
public Guid CompanyTableId { get; set; }
public DbCompanyTable CompanyTable { get; set; }
public ICollection<DbProduct> Products { get; set; }
public ICollection<DbCompanyRating> CompanyRatings { get; set; }
public string CompanyName { get; set; }
public long RatingSum { get; set; }
public long RatingCount { get; set; }
}

View file

@ -3,10 +3,10 @@ namespace DrinkRateAPI.DbEntities;
public class DbCompanyRating
{
public Guid UserId { get; set; }
public DbUser User { get; set; }
public Guid CompanyId { get; set; }
public Guid CompanyTableId { get; set; }
public DbCompany Company { get; set; }
public byte Rating { get; set; }

View file

@ -2,9 +2,9 @@ namespace DrinkRateAPI.DbEntities;
public class DbCompanyTable
{
public string TableName { get; set; }
public Guid Id { get; set; }
public ICollection<DbCompany> Companies { get; set; }
public ICollection<DbCompanyRating> CompanyRatings { get; set; }
public string CompanyTable { get; set; }
}

View file

@ -4,7 +4,7 @@ public class DbCompanyTableView
{
public Guid Id { get; set; }
public Guid CompanyTableId { get; set; }
public ICollection<DbCompanyTable> CompanyTables { get; set; }
public ICollection<DbUser> Users { get; set; }

View file

@ -4,9 +4,17 @@ public class DbProduct
{
public Guid Id { get; set; }
public Guid CompanyId { get; set; }
public Guid ProductTableId { get; set; }
public DbProductTable ProductTable { get; set; }
public Guid TableId { get; set; }
public Guid CompanyId { get; set; }
public DbCompany Company { get; set; }
public ICollection<DbProductRating> ProductRatings { get; set; }
public string ProductName { get; set; }
public long RatingSum { get; set; }
public long RatingCount { get; set; }
}

View file

@ -3,10 +3,10 @@ namespace DrinkRateAPI.DbEntities;
public class DbProductRating
{
public Guid UserId { get; set; }
public DbUser User { get; set; }
public Guid ProductId { get; set; }
public Guid ProductTableId { get; set; }
public DbProduct Product { get; set; }
public byte Rating { get; set; }

View file

@ -4,9 +4,7 @@ public class DbProductTable
{
public Guid Id { get; set; }
public string TableName { get; set; }
public ICollection<DbProduct> Products { get; set; }
public ICollection<DbProductRating> ProductRatings { get; set; }
public string ProductTable { get; set; }
}

View file

@ -4,10 +4,9 @@ public class DbProductTableView
{
public Guid Id { get; set; }
public Guid ProductTableId { get; set; }
public ICollection<DbProductTable> ProductTables { get; set; }
public ICollection<DbUser> Users { get; set; }
// to do: permission types
}

View file

@ -10,8 +10,12 @@ public class DbUser
public ICollection<DbCompanyRating> CompanyRatings { get; set; }
public ICollection<DbProductRating> ProductRatings { get; set; }
public ICollection<DbUserCompanyTableStats> UserCompanyTableStats { get; set; }
public ICollection<DbUserProductTableStats> UserProductTableStats { get; set; }
public ICollection<DbUserCompanyTableStat> UserCompanyTableStats { get; set; }
public ICollection<DbUserProductTableStat> UserProductTableStats { get; set; }
public string UserName { get; set; }
public bool IsAdmin { get; set; }
public bool IsDeleted { get; set; }
}

View file

@ -0,0 +1,16 @@
namespace DrinkRateAPI.DbEntities;
public class DbUserCompanyTableStat
{
public Guid UserId { get; set; }
public DbUser User { get; set; }
public Guid CompanyTableId { get; set; }
public DbCompanyTable CompanyTable { get; set; }
public int RatingCount { get; set; }
public int HighestRatingCount { get; set; }
public int Credits { get; set; }
}

View file

@ -1,14 +0,0 @@
namespace DrinkRateAPI.DbEntities;
public class DbUserCompanyTableStats
{
public Guid UserId { get; set; }
public Guid CompanyTabelId { get; set; }
public int Count { get; set; }
public int MaxCount { get; set; }
public int Credits { get; set; }
}

View file

@ -0,0 +1,16 @@
namespace DrinkRateAPI.DbEntities;
public class DbUserProductTableStat
{
public Guid UserId { get; set; }
public DbUser User { get; set; }
public Guid ProductTableId { get; set; }
public DbProductTable ProductTable { get; set; }
public int RatingCount { get; set; }
public int HighestRatingCount { get; set; }
public int Credits { get; set; }
}

View file

@ -1,14 +0,0 @@
namespace DrinkRateAPI.DbEntities;
public class DbUserProductTableStats
{
public Guid UserId { get; set; }
public Guid ProductTabelId { get; set; }
public int Count { get; set; }
public int MaxCount { get; set; }
public int Credits { get; set; }
}

View file

@ -9,7 +9,13 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.8" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View file

@ -0,0 +1,29 @@
// <auto-generated />
using DrinkRateAPI.Contexts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DrinkRateAPI.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20250809085523_250809_ApplicationInit")]
partial class _250809_ApplicationInit
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.8")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
#pragma warning restore 612, 618
}
}
}

View file

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DrinkRateAPI.Migrations
{
/// <inheritdoc />
public partial class _250809_ApplicationInit : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View file

@ -0,0 +1,26 @@
// <auto-generated />
using DrinkRateAPI.Contexts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DrinkRateAPI.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.8")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
#pragma warning restore 612, 618
}
}
}

View file

@ -4,5 +4,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"Local": "Host=localhost;Port=6942;Database=postgres;Username=postgres;Password=rum_beer_quests_mead"
}
}

View file

@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"Local": "Host=localhost;Port=6942;Database=postgres;Username=postgres;Password=rum_beer_quests_mead"
}
}

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -4,5 +4,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"Local": "Host=localhost;Port=6942;Database=postgres;Username=postgres;Password=rum_beer_quests_mead"
}
}

View file

@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"Local": "Host=localhost;Port=6942;Database=postgres;Username=postgres;Password=rum_beer_quests_mead"
}
}

Some files were not shown because too many files have changed in this diff Show more