c#
namespace HTXL_Back_end.Models;
public class HTXLPortfolioContext : DbContext
{
public HTXLPortfolioContext()
{
}
public HTXLPortfolioContext(DbContextOptions<HTXLPortfolioContext> options)
: base(options)
{
}
public DbSet<Portfolio> Portfolios { get; set; }
public DbSet<Venture> Ventures { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Server=LAPTOP-HU4NMC01;Database=HighTechXL;Trusted_Connection=true;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Portfolio>()
.Property(s => s.Id)
.HasDefaultValueSql("NEWID()");
modelBuilder.Entity<Venture>()
.Property(s => s.Id)
.HasDefaultValueSql("NEWID()");
// Configure a many-to-many relationship between Portfolios and Sdgs.
modelBuilder.Entity<Portfolio>()
.HasMany(e => e.Sdgs)
.WithMany(e => e.Portfolios);
// Set Created at value to be automaticly created at creation.
modelBuilder.Entity<Portfolio>().Property(x => x.CreatedAt)
.HasDefaultValueSql("getdate() AT TIME ZONE 'Central European Standard Time'");
// Set Updated at velue when row is updated. Doesn't actually work with sql database because sql is stupid!
modelBuilder.Entity<Portfolio>().Property(x => x.UpdatedAt)
.HasDefaultValueSql("getdate()")
.ValueGeneratedOnAddOrUpdate()
.Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Save);
// Configure the Portfolio entity to use a temporal table, enabling tracking of data changes over time.
modelBuilder.Entity<Portfolio>().ToTable("Portfolio", e => e.IsTemporal());
}
}