0b357e1e13
Removed strongly typed inventory objects Added EAV inventory types Added EAV object handling philosophy Added controllers Added EF Core migration integration Reviewed-on: #1
45 lines
1.7 KiB
C#
45 lines
1.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using WyvernInventory.Core.Models;
|
|
|
|
namespace WyvernInventory.Infrastructure.Data;
|
|
|
|
public class DBContext : DbContext
|
|
{
|
|
|
|
public DBContext(DbContextOptions<DBContext> options) : base(options) {}
|
|
|
|
public DbSet<InventoryItem> InventoryItems => Set<InventoryItem>();
|
|
public DbSet<InventoryAttributeDefinition> InventoryAttributeDefinitions => Set<InventoryAttributeDefinition>();
|
|
public DbSet<InventoryAttributeValue> InventoryAttributeValues => Set<InventoryAttributeValue>();
|
|
public DbSet<InventoryType> InventoryTypes => Set<InventoryType>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<InventoryItem>()
|
|
.HasOne(_ => _.Type)
|
|
.WithMany()
|
|
.HasForeignKey(_ => _.TypeId);
|
|
|
|
modelBuilder.Entity<InventoryAttributeValue>()
|
|
.HasOne(_ => _.Item)
|
|
.WithMany(_ => _.AttributeValues)
|
|
.HasForeignKey(_ => _.ItemId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
modelBuilder.Entity<InventoryAttributeValue>()
|
|
.HasOne(_ => _.AttributeDefinition)
|
|
.WithMany()
|
|
.HasForeignKey(_ => _.AttributeDefinitionId)
|
|
.OnDelete(DeleteBehavior.NoAction);
|
|
|
|
modelBuilder.Entity<InventoryAttributeDefinition>()
|
|
.HasOne(_ => _.Type)
|
|
.WithMany(_ => _.AttributeDefinitions)
|
|
.HasForeignKey(_ => _.TypeId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
modelBuilder.Entity<InventoryAttributeValue>()
|
|
.HasIndex(_ => new { _.ItemId, _.AttributeDefinitionId})
|
|
.IsUnique();
|
|
}
|
|
} |