Skip to content

Commit fee483d

Browse files
author
Simon Gábor
committed
.net 5.0 transition
1 parent c14a01a commit fee483d

12 files changed

Lines changed: 144 additions & 95 deletions

File tree

WebApiLab.BLL/DTOs/Product.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections.Generic;
2-
using WebApiLab.Entities;
2+
using WebApiLab.DAL.Entities;
33

44
namespace WebApiLab.API.DTO
55
{

WebApiLab.BLL/Interfaces/IProductService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections.Generic;
2-
using WebApiLab.Entities;
2+
using WebApiLab.DAL.Entities;
33

44
namespace WebApiLab.BLL
55
{

WebApiLab.BLL/WebApiLab.BLL.csproj

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55
</PropertyGroup>
66

7-
<ItemGroup>
8-
<Folder Include="Exceptions\" />
9-
<Folder Include="DTOs\" />
10-
</ItemGroup>
11-
127
<ItemGroup>
138
<ProjectReference Include="..\WebApiLab.DAL\WebApiLab.DAL.csproj" />
149
</ItemGroup>

WebApiLab.DAL/Entities/Category.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
26

3-
namespace WebApiLab.Entities
7+
namespace WebApiLab.DAL.Entities
48
{
59
public class Category
610
{
711
public int Id { get; set; }
812
public string Name { get; set; }
913

10-
public ICollection<Product> Products { get; } = new List<Product>();
14+
public ICollection<Product> Products { get; }
15+
= new List<Product>();
1116
}
1217
}

WebApiLab.DAL/Entities/Order.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
36

4-
namespace WebApiLab.Entities
7+
namespace WebApiLab.DAL.Entities
58
{
69
public class Order
710
{
811
public int Id { get; set; }
12+
913
public DateTime OrderDate { get; set; }
1014

11-
public ICollection<ProductOrder> ProductOrders { get; } = new List<ProductOrder>();
15+
public ICollection<OrderItem> OrderItems { get; }
16+
= new List<OrderItem>();
17+
18+
public ICollection<Product> Products { get; }
19+
= new List<Product>();
1220
}
1321
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace WebApiLab.DAL.Entities
8+
{
9+
public class OrderItem
10+
{
11+
public int Id { get; set; }
12+
13+
public int ProductId { get; set; }
14+
15+
public Product Product { get; set; }
16+
17+
public int OrderId { get; set; }
18+
19+
public Order Order { get; set; }
20+
21+
public int Quantity { get; set; }
22+
23+
}
24+
}

WebApiLab.DAL/Entities/Product.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
27

3-
4-
namespace WebApiLab.Entities
8+
namespace WebApiLab.DAL.Entities
59
{
610
public class Product
711
{
812
public int Id { get; set; }
13+
14+
[Column("ProductName")]
915
public string Name { get; set; }
1016
public int UnitPrice { get; set; }
11-
public ShipmentRegion ShipmentRegion { get; set; }
1217

1318
public int CategoryId { get; set; }
1419
public Category Category { get; set; }
1520

16-
public ICollection<ProductOrder> ProductOrders { get; } = new List<ProductOrder>();
21+
public ICollection<OrderItem> ProductOrders { get; }
22+
= new List<OrderItem>();
23+
24+
public ICollection<Order> Orders { get; } = new List<Order>();
25+
26+
public ShipmentRegion ShipmentRegion { get; set; }
1727
}
28+
1829
}

WebApiLab.DAL/Entities/ProductOrder.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

WebApiLab.DAL/Entities/ShipmentRegion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace WebApiLab.Entities
3+
namespace WebApiLab.DAL.Entities
44
{
55
[Flags]
66
public enum ShipmentRegion

WebApiLab.DAL/NorthwindContext.cs

Lines changed: 67 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,95 @@
1-
using System;
2-
using Microsoft.EntityFrameworkCore;
3-
using Microsoft.EntityFrameworkCore.Diagnostics;
1+
using Microsoft.EntityFrameworkCore;
42
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
5-
using Microsoft.Extensions.DependencyInjection;
63
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;
810

911
namespace WebApiLab.DAL
1012
{
1113
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+
}
1720

1821
protected override void OnModelCreating(ModelBuilder modelBuilder)
1922
{
2023
base.OnModelCreating(modelBuilder);
21-
22-
modelBuilder.Entity<Category>()
24+
25+
modelBuilder.Entity<Category>()
2326
.Property(c => c.Name)
2427
.HasMaxLength(15)
2528
.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+
);
2833

2934
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 },
3038
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+
);
5655

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+
);
5959

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+
);
6364

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+
});
6882

6983
var converter = new EnumToStringConverter<ShipmentRegion>();
7084
modelBuilder
7185
.Entity<Product>()
7286
.Property(e => e.ShipmentRegion)
7387
.HasConversion(converter);
7488
}
89+
90+
91+
public DbSet<Product> Products { get; set; }
92+
public DbSet<Category> Categories { get; set; }
93+
public DbSet<Order> Orders { get; set; }
7594
}
7695
}

0 commit comments

Comments
 (0)