Have made an migration and a DB context to make sure it does create all necessary tables

This commit is contained in:
Francesco D'Amico
2025-01-06 17:01:30 +01:00
parent b80a402d15
commit 6e9ce70355
4 changed files with 1284 additions and 0 deletions
@@ -0,0 +1,47 @@
namespace Francesco.Recipes.World.Data
{
using Francesco.Recipes.World.Models.BackendModels.Category;
using Francesco.Recipes.World.Models.BackendModels.Ingredient;
using Francesco.Recipes.World.Models.BackendModels.Instruction;
using Francesco.Recipes.World.Models.BackendModels.Recipe;
using Francesco.Recipes.World.Models.BackendModels.RecipeIngredient;
using Francesco.Recipes.World.Models.BackendModels;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
public class FrancescosRecipesWorldDbContext : IdentityDbContext<IdentityUser>
{
public FrancescosRecipesWorldDbContext(DbContextOptions<FrancescosRecipesWorldDbContext> options)
: base(options)
{
}
public DbSet<Category> Categories => Set<Category>();
public DbSet<Ingredient> Ingredients => Set<Ingredient>();
public DbSet<Instruction> Instructions => Set<Instruction>();
public DbSet<Recipe> Recipes => Set<Recipe>();
public DbSet<RecipeIngredient> RecipeIngredients => Set<RecipeIngredient>();
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
foreach (var entry in ChangeTracker.Entries())
{
if (entry.Entity is ITimeStampedEntity timeStampedEntity)
{
switch (entry.State)
{
case EntityState.Added:
timeStampedEntity.CreatedAt = DateTime.UtcNow;
break;
case EntityState.Modified:
timeStampedEntity.ModifiedAt = DateTime.UtcNow;
break;
}
}
}
return base.SaveChangesAsync(cancellationToken);
}
}
}