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