-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoTemporelleContext.cs
75 lines (61 loc) · 2.19 KB
/
DemoTemporelleContext.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
using Microsoft.EntityFrameworkCore;
namespace EfCoreTemporalTablePart3.Model;
public partial class DemoTemporelleContext : DbContext
{
public DemoTemporelleContext()
{
}
public DemoTemporelleContext(DbContextOptions<DemoTemporelleContext> options)
: base(options)
{
}
public virtual DbSet<Employe> Employe { get; set; }
public virtual DbSet<Entreprise> Entreprise { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("data source=.;initial catalog=DemoTemporelleEf6;Integrated Security=SSPI");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Définitions sans temporalité
modelBuilder.Entity<Employe>(entity =>
{
entity.Property(e => e.Nom)
.IsRequired()
.HasMaxLength(80);
entity.Property(e => e.Prenom)
.IsRequired()
.HasMaxLength(80);
entity.HasOne(d => d.Entreprise)
.WithMany(p => p.Employe)
.HasForeignKey(d => d.EntrepriseId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Employe_Entreprise");
});
modelBuilder.Entity<Entreprise>(entity =>
{
entity.Property(e => e.Adresse).HasMaxLength(80);
entity.Property(e => e.Nom).HasMaxLength(80);
});
//Ajout temporalités
modelBuilder
.Entity<Employe>()
.ToTable("Employe", b => b.IsTemporal());
modelBuilder.Entity<Entreprise>()
.ToTable(
"Entreprise",
b => b.IsTemporal(
b =>
{
//Changement des noms par défaut
b.HasPeriodStart("ValideDu");
b.HasPeriodEnd("ValideAu");
b.UseHistoryTable("EntrepriseHistorique");
}));
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}