-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathApplicationDbContext.cs
More file actions
57 lines (45 loc) · 1.55 KB
/
ApplicationDbContext.cs
File metadata and controls
57 lines (45 loc) · 1.55 KB
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
using System;
using Microsoft.EntityFrameworkCore;
using DeveloperTest.Database.Models;
namespace DeveloperTest.Database
{
public class ApplicationDbContext : DbContext
{
public DbSet<Job> Jobs { get; set; }
public DbSet<Customer> Customers { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.HasKey(x => x.CustomerId);
modelBuilder.Entity<Customer>()
.Property(x => x.CustomerId)
.ValueGeneratedOnAdd();
modelBuilder.Entity<Customer>()
.HasData(new Customer
{
CustomerId = 1,
Name = "TestCustomer",
Type = CustomerType.Small
});
modelBuilder.Entity<Job>()
.HasKey(x => x.JobId);
modelBuilder.Entity<Job>()
.Property(x => x.JobId)
.ValueGeneratedOnAdd();
modelBuilder.Entity<Job>()
.HasOne(j => j.Customer)
.WithMany(c => c.Jobs);
modelBuilder.Entity<Job>()
.HasData(new Job
{
JobId = 1,
Engineer = "Test",
When = new DateTime(2022, 2, 1, 12, 0, 0)
});
base.OnModelCreating(modelBuilder);
}
}
}