|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | + |
| 5 | +using WebApiLab.Dal.Entities; |
| 6 | + |
| 7 | +namespace WebApiLab.Dal; |
| 8 | + |
| 9 | +public class AppDbContext : DbContext |
| 10 | +{ |
| 11 | + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| 12 | + { |
| 13 | + optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=NEPTUN;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False") |
| 14 | + .LogTo(Console.WriteLine, LogLevel.Debug); |
| 15 | + } |
| 16 | + |
| 17 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 18 | + { |
| 19 | + base.OnModelCreating(modelBuilder); |
| 20 | + |
| 21 | + modelBuilder.Entity<Category>() |
| 22 | + .Property(c => c.Name) |
| 23 | + .HasMaxLength(15) |
| 24 | + .IsRequired(); |
| 25 | + |
| 26 | + modelBuilder.Entity<Category>().HasData( |
| 27 | + new Category("Ital") { Id = 1 } |
| 28 | + ); |
| 29 | + |
| 30 | + modelBuilder.Entity<Product>().HasData( |
| 31 | + new Product("Sör") { Id = 1, UnitPrice = 50, CategoryId = 1, ShipmentRegion = ShipmentRegion.Asia }, |
| 32 | + new Product("Bor") { Id = 2, UnitPrice = 550, CategoryId = 1 }, |
| 33 | + new Product("Tej") { Id = 3, UnitPrice = 260, CategoryId = 1 }, |
| 34 | + new Product("Whiskey") |
| 35 | + { |
| 36 | + Id = 4, |
| 37 | + UnitPrice = 960, |
| 38 | + CategoryId = 1, |
| 39 | + ShipmentRegion = ShipmentRegion.Australia |
| 40 | + }, |
| 41 | + new Product("Rum") |
| 42 | + { |
| 43 | + Id = 5, |
| 44 | + UnitPrice = 960, |
| 45 | + CategoryId = 1, |
| 46 | + ShipmentRegion = ShipmentRegion.Eu | ShipmentRegion.NorthAmerica |
| 47 | + } |
| 48 | + ); |
| 49 | + |
| 50 | + modelBuilder.Entity<Order>().HasData( |
| 51 | + new Order { Id = 1, OrderDate = new DateTime(2019, 02, 01) } |
| 52 | + ); |
| 53 | + |
| 54 | + modelBuilder.Entity<OrderItem>().HasData( |
| 55 | + new OrderItem { Id = 1, OrderId = 1, ProductId = 1 }, |
| 56 | + new OrderItem { Id = 2, OrderId = 1, ProductId = 2 } |
| 57 | + ); |
| 58 | + |
| 59 | + modelBuilder.Entity<Product>() |
| 60 | + .HasMany(p => p.Orders) |
| 61 | + .WithMany(o => o.Products) |
| 62 | + .UsingEntity<OrderItem>( |
| 63 | + j => j |
| 64 | + .HasOne(oi => oi.Order) |
| 65 | + .WithMany(o => o.OrderItems) |
| 66 | + .HasForeignKey(oi => oi.OrderId) |
| 67 | + .OnDelete(DeleteBehavior.Restrict), |
| 68 | + j => j |
| 69 | + .HasOne(oi => oi.Product) |
| 70 | + .WithMany(p => p.ProductOrders) |
| 71 | + .HasForeignKey(oi => oi.ProductId), |
| 72 | + j => |
| 73 | + { |
| 74 | + j.HasKey(oi => oi.Id); |
| 75 | + }); |
| 76 | + |
| 77 | + modelBuilder |
| 78 | + .Entity<Product>() |
| 79 | + .Property(e => e.ShipmentRegion) |
| 80 | + .HasConversion(new EnumToStringConverter<ShipmentRegion>()); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + public DbSet<Product> Products => Set<Product>(); |
| 85 | + public DbSet<Category> Categories => Set<Category>(); |
| 86 | + public DbSet<Order> Orders => Set<Order>(); |
| 87 | +} |
0 commit comments