Refactor. Add data layer
This commit is contained in:
1 parent
3b7e4e6ec5
commit
b6b794f0b4
82 files changed
+2930
-2811
No files matched your search
@@ -0,0 +1,77 @@
|
||||
using BudgetApp.Data.Entities;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BudgetApp.Data;
|
||||
|
||||
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
|
||||
: IdentityDbContext<ApplicationUser>(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)");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user