-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathCompositeDbContext.cs
42 lines (34 loc) · 1.2 KB
/
CompositeDbContext.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
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
// @formatter:wrap_chained_method_calls chop_always
namespace JsonApiDotNetCoreTests.IntegrationTests.CompositeKeys;
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class CompositeDbContext : DbContext
{
public DbSet<Car> Cars => Set<Car>();
public DbSet<Engine> Engines => Set<Engine>();
public DbSet<Dealership> Dealerships => Set<Dealership>();
public CompositeDbContext(DbContextOptions<CompositeDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Car>()
.HasKey(car => new
{
car.RegionId,
car.LicensePlate
});
builder.Entity<Engine>()
.HasOne(engine => engine.Car)
.WithOne(car => car.Engine)
.HasForeignKey<Engine>();
builder.Entity<Dealership>()
.HasMany(dealership => dealership.Inventory)
.WithOne(car => car.Dealership!);
builder.Entity<Car>()
.HasMany(car => car.PreviousDealerships)
.WithMany(dealership => dealership.SoldCars);
}
}