-
Notifications
You must be signed in to change notification settings - Fork 0
/
BugDbContext.cs
77 lines (71 loc) · 4.04 KB
/
BugDbContext.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
using EF.ComplexPropertyBug.Models;
using Microsoft.EntityFrameworkCore;
namespace EF.ComplexPropertyBug;
public class BugDbContext(DbContextOptions<BugDbContext> options) : DbContext(options)
{
public DbSet<Shipment> Shipments { get; set; }
public DbSet<OrderPosition> OrderPositions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Shipment>()
.HasKey(s => s.Id);
modelBuilder.Entity<Shipment>()
.ComplexProperty(
shipment => shipment.Recipient,
complexPropertyBuilder => complexPropertyBuilder
.ComplexProperty(
recipient => recipient.Address,
addressBuilder => addressBuilder.Property(address => address.Id)
.HasColumnName("AddressId")
.HasConversion(
convertToProviderExpression: id => id.Value.UnwrapOr(0),
convertFromProviderExpression: value => Id.CreateWithoutValidation<Address>(value))
.IsRequired())
.ComplexProperty(
recipient => recipient.Customer,
customerBuilder => customerBuilder.Property(customer => customer.Id)
.HasColumnName("CustomerId")
.HasConversion(
convertToProviderExpression: id => id.Value.UnwrapOr(0),
convertFromProviderExpression: value => Id.CreateWithoutValidation<Customer>(value))
.IsRequired())
.ComplexProperty(
recipient => recipient.Person,
personBuilder => personBuilder.Property(person => person.Id)
.HasColumnName("Person")
.HasConversion(
convertToProviderExpression: id => id.Value.UnwrapOr(0),
convertFromProviderExpression: value => Id.CreateWithoutValidation<Person>(value))
.IsRequired()));
modelBuilder.Entity<OrderPosition>()
.HasKey(o => o.Id);
modelBuilder.Entity<OrderPosition>()
.ComplexProperty(
shipment => shipment.Recipient,
complexPropertyBuilder => complexPropertyBuilder
.ComplexProperty(
recipient => recipient.Address,
addressBuilder => addressBuilder.Property(address => address.Id)
.HasColumnName("AddressId")
.HasConversion(
convertToProviderExpression: id => id.Value.UnwrapOr(0),
convertFromProviderExpression: value => Id.CreateWithoutValidation<Address>(value))
.IsRequired())
.ComplexProperty(
recipient => recipient.Customer,
customerBuilder => customerBuilder.Property(customer => customer.Id)
.HasColumnName("CustomerId")
.HasConversion(
convertToProviderExpression: id => id.Value.UnwrapOr(0),
convertFromProviderExpression: value => Id.CreateWithoutValidation<Customer>(value))
.IsRequired())
.ComplexProperty(
recipient => recipient.Person,
buyerPersonBuilder => buyerPersonBuilder.Property(person => person.Id)
.HasColumnName("PersonId")
.HasConversion(
convertToProviderExpression: id => id.Value.UnwrapOr(0),
convertFromProviderExpression: value => Id.CreateWithoutValidation<Person>(value))
.IsRequired()));
}
}