Files
Budgeteer/BudgetApp/Data/ApplicationDbContext.cs
T
2026-03-05 18:57:12 +00:00

81 lines
2.4 KiB
C#

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace BudgetApp.Data;
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Account> Accounts => Set<Account>();
public DbSet<Transaction> Transactions => Set<Transaction>();
public DbSet<Category> Categories => Set<Category>();
public DbSet<Salary> Salaries => Set<Salary>();
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Account>(e =>
{
e.HasOne(a => a.User)
.WithMany()
.HasForeignKey(a => a.UserId)
.OnDelete(DeleteBehavior.Cascade);
e.Property(a => a.CurrentBalance).HasColumnType("decimal(18,2)");
});
builder.Entity<Transaction>(e =>
{
e.HasOne(t => t.Account)
.WithMany(a => a.Transactions)
.HasForeignKey(t => t.AccountId)
.OnDelete(DeleteBehavior.Cascade);
e.HasOne(t => t.Category)
.WithMany(c => c.Transactions)
.HasForeignKey(t => t.CategoryId)
.OnDelete(DeleteBehavior.SetNull);
e.HasOne(t => t.ParentTransaction)
.WithMany()
.HasForeignKey(t => t.ParentTransactionId)
.OnDelete(DeleteBehavior.SetNull);
e.HasOne(t => t.Salary)
.WithMany()
.HasForeignKey(t => t.SalaryId)
.OnDelete(DeleteBehavior.SetNull);
e.Property(t => t.Amount).HasColumnType("decimal(18,2)");
});
builder.Entity<Category>(e =>
{
e.HasOne(c => c.User)
.WithMany()
.HasForeignKey(c => c.UserId)
.OnDelete(DeleteBehavior.Cascade);
});
builder.Entity<Salary>(e =>
{
e.HasOne(s => s.User)
.WithMany()
.HasForeignKey(s => s.UserId)
.OnDelete(DeleteBehavior.Cascade);
e.HasOne(s => s.Account)
.WithMany()
.HasForeignKey(s => s.AccountId)
.OnDelete(DeleteBehavior.Cascade);
e.Property(s => s.Amount).HasColumnType("decimal(18,2)");
});
}
}