127 lines
5.6 KiB
C#
127 lines
5.6 KiB
C#
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Webshop.Domain.Entities;
|
|
|
|
namespace Webshop.Infrastructure.Data
|
|
{
|
|
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
|
|
{
|
|
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
|
|
{
|
|
}
|
|
|
|
public DbSet<Product> Products { get; set; } = default!;
|
|
public DbSet<ProductVariant> ProductVariants { get; set; } = default!;
|
|
public DbSet<Category> Categories { get; set; } = default!;
|
|
public DbSet<Customer> Customers { get; set; } = default!;
|
|
public DbSet<Address> Addresses { get; set; } = default!;
|
|
public DbSet<Order> Orders { get; set; } = default!;
|
|
public DbSet<OrderItem> OrderItems { get; set; } = default!;
|
|
public DbSet<Review> Reviews { get; set; } = default!;
|
|
public DbSet<Discount> Discounts { get; set; } = default!;
|
|
public DbSet<Supplier> Suppliers { get; set; } = default!;
|
|
public DbSet<ShippingMethod> ShippingMethods { get; set; } = default!;
|
|
public DbSet<PaymentMethod> PaymentMethods { get; set; } = default!;
|
|
public DbSet<Setting> Settings { get; set; } = default!;
|
|
|
|
public DbSet<ProductCategory> ProductCategories { get; set; } = default!;
|
|
public DbSet<ProductDiscount> ProductDiscounts { get; set; } = default!;
|
|
public DbSet<CategoryDiscount> CategoryDiscounts { get; set; } = default!;
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
foreach (var entity in modelBuilder.Model.GetEntityTypes())
|
|
{
|
|
var tableName = entity.GetTableName();
|
|
|
|
if (tableName != null && tableName.StartsWith("AspNet"))
|
|
{
|
|
entity.SetTableName(tableName.Substring(6));
|
|
}
|
|
}
|
|
|
|
modelBuilder.Entity<ProductCategory>().HasKey(pc => new { pc.ProductId, pc.CategoryId });
|
|
modelBuilder.Entity<ProductDiscount>().HasKey(pd => new { pd.ProductId, pd.DiscountId });
|
|
modelBuilder.Entity<CategoryDiscount>().HasKey(cd => new { cd.CategoryId, cd.DiscountId });
|
|
|
|
modelBuilder.Entity<Product>().HasIndex(p => p.SKU).IsUnique();
|
|
modelBuilder.Entity<Product>().HasIndex(p => p.Slug).IsUnique();
|
|
modelBuilder.Entity<Category>().HasIndex(c => c.Slug).IsUnique();
|
|
modelBuilder.Entity<Discount>().HasIndex(d => d.CouponCode).IsUnique().HasFilter("\"CouponCode\" IS NOT NULL");
|
|
modelBuilder.Entity<Setting>().HasIndex(s => s.Key).IsUnique();
|
|
modelBuilder.Entity<Order>().HasIndex(o => o.OrderNumber).IsUnique();
|
|
|
|
modelBuilder.Entity<Product>(e => {
|
|
e.Property(p => p.Price).HasPrecision(18, 2);
|
|
e.Property(p => p.OldPrice).HasPrecision(18, 2);
|
|
e.Property(p => p.PurchasePrice).HasPrecision(18, 2);
|
|
e.Property(p => p.Weight).HasPrecision(18, 3);
|
|
e.Property(p => p.Width).HasPrecision(18, 2);
|
|
e.Property(p => p.Height).HasPrecision(18, 2);
|
|
e.Property(p => p.Length).HasPrecision(18, 2);
|
|
});
|
|
|
|
modelBuilder.Entity<ProductVariant>()
|
|
.Property(pv => pv.PriceAdjustment).HasPrecision(18, 2);
|
|
|
|
modelBuilder.Entity<Order>(e => {
|
|
e.Property(o => o.OrderTotal).HasPrecision(18, 2);
|
|
e.Property(o => o.ShippingCost).HasPrecision(18, 2);
|
|
e.Property(o => o.TaxAmount).HasPrecision(18, 2);
|
|
e.Property(o => o.DiscountAmount).HasPrecision(18, 2);
|
|
});
|
|
|
|
modelBuilder.Entity<OrderItem>(e => {
|
|
e.Property(oi => oi.UnitPrice).HasPrecision(18, 2);
|
|
e.Property(oi => oi.TotalPrice).HasPrecision(18, 2);
|
|
});
|
|
|
|
modelBuilder.Entity<Discount>(e => {
|
|
e.Property(d => d.DiscountValue).HasPrecision(18, 2);
|
|
e.Property(d => d.MinimumOrderAmount).HasPrecision(18, 2);
|
|
});
|
|
|
|
modelBuilder.Entity<ShippingMethod>(e => {
|
|
e.Property(sm => sm.BaseCost).HasPrecision(18, 2);
|
|
e.Property(sm => sm.MinimumOrderAmount).HasPrecision(18, 2);
|
|
});
|
|
|
|
modelBuilder.Entity<PaymentMethod>()
|
|
.Property(pm => pm.ProcessingFee).HasPrecision(18, 2);
|
|
|
|
modelBuilder.Entity<Category>()
|
|
.HasOne(c => c.ParentCategory)
|
|
.WithMany(c => c.SubCategories)
|
|
.HasForeignKey(c => c.ParentCategoryId)
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
|
|
modelBuilder.Entity<OrderItem>()
|
|
.HasOne(oi => oi.Product)
|
|
.WithMany()
|
|
.HasForeignKey(oi => oi.ProductId)
|
|
.OnDelete(DeleteBehavior.SetNull);
|
|
|
|
modelBuilder.Entity<OrderItem>()
|
|
.HasOne(oi => oi.ProductVariant)
|
|
.WithMany()
|
|
.HasForeignKey(oi => oi.ProductVariantId)
|
|
.OnDelete(DeleteBehavior.SetNull);
|
|
|
|
modelBuilder.Entity<Order>()
|
|
.HasOne(o => o.BillingAddress)
|
|
.WithMany()
|
|
.HasForeignKey(o => o.BillingAddressId)
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
|
|
modelBuilder.Entity<Order>()
|
|
.HasOne(o => o.ShippingAddress)
|
|
.WithMany()
|
|
.HasForeignKey(o => o.ShippingAddressId)
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
}
|
|
}
|
|
} |