-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderConfiguration.cs
118 lines (92 loc) · 3.65 KB
/
OrderConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Ordering.Infrastructure.Data.Configurations;
public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Id).HasConversion(
orderId => orderId.Value,
dbId => OrderId.Of(dbId));
builder.HasOne<Customer>()
.WithMany()
.HasForeignKey(o => o.CustomerId)
.IsRequired();
builder.HasMany(o => o.OrderItems)
.WithOne()
.HasForeignKey(oi => oi.OrderId);
builder.ComplexProperty(
o => o.OrderName, nameBuilder =>
{
nameBuilder.Property(n => n.Value)
.HasColumnName(nameof(Order.OrderName))
.HasMaxLength(100)
.IsRequired();
});
builder.ComplexProperty(
o => o.ShippingAddress, addressBuilder =>
{
addressBuilder.Property(a => a.FirstName)
.HasMaxLength(50)
.IsRequired();
addressBuilder.Property(a => a.LastName)
.HasMaxLength(50)
.IsRequired();
addressBuilder.Property(a => a.EmailAddress)
.HasMaxLength(50);
addressBuilder.Property(a => a.AddressLine)
.HasMaxLength(180)
.IsRequired();
addressBuilder.Property(a => a.Country)
.HasMaxLength(50);
addressBuilder.Property(a => a.State)
.HasMaxLength(50);
addressBuilder.Property(a => a.ZipCode)
.HasMaxLength(5)
.IsRequired();
});
builder.ComplexProperty(
o => o.BillingAddress, addressBuilder =>
{
addressBuilder.Property(a => a.FirstName)
.HasMaxLength(50)
.IsRequired();
addressBuilder.Property(a => a.LastName)
.HasMaxLength(50)
.IsRequired();
addressBuilder.Property(a => a.EmailAddress)
.HasMaxLength(50);
addressBuilder.Property(a => a.AddressLine)
.HasMaxLength(180)
.IsRequired();
addressBuilder.Property(a => a.Country)
.HasMaxLength(50);
addressBuilder.Property(a => a.State)
.HasMaxLength(50);
addressBuilder.Property(a => a.ZipCode)
.HasMaxLength(5)
.IsRequired();
});
builder.ComplexProperty(
o => o.Payment, paymentBuilder =>
{
paymentBuilder.Property(p => p.CardName)
.HasMaxLength(50);
paymentBuilder.Property(p => p.CardNumber)
.HasMaxLength(24)
.IsRequired();
paymentBuilder.Property(p => p.Expiration)
.HasMaxLength(10);
paymentBuilder.Property(p => p.CVV)
.HasMaxLength(3);
paymentBuilder.Property(p => p.PaymentMethod);
});
builder.Property(o => o.Status)
.HasDefaultValue(OrderStatus.Draft)
.HasConversion(
s => s.ToString(),
dbStatus => (OrderStatus)Enum.Parse(typeof(OrderStatus), dbStatus));
builder.Property(o => o.TotalPrice);
}
}