This repository has been archived by the owner on Mar 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
SampleContext.cs
114 lines (86 loc) · 3.88 KB
/
SampleContext.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
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EFSecondLevelCache.Core.AspNetCoreSample.DataLayer.Entities;
using EFSecondLevelCache.Core.Contracts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace EFSecondLevelCache.Core.AspNetCoreSample.DataLayer
{
public class SampleContext : DbContext
{
public virtual DbSet<Post> Posts { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<TagProduct> TagProducts { get; set; }
public virtual DbSet<Tag> Tags { get; set; }
public virtual DbSet<User> Users { get; set; }
public EFCachedDbSet<Post> CachedPosts => this.Set<Post>().Cacheable();
public SampleContext(DbContextOptions<SampleContext> options) : base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>(entity =>
{
entity.HasIndex(e => e.UserId);
entity.HasOne(d => d.User)
.WithMany(p => p.Posts)
.HasForeignKey(d => d.UserId);
entity.HasDiscriminator<string>("post_type")
.HasValue<Post>("post_base")
.HasValue<Page>("post_page");
});
modelBuilder.Entity<Product>(entity =>
{
entity.HasKey(e => e.ProductId);
entity.HasIndex(e => e.ProductName)
.IsUnique();
entity.HasIndex(e => e.UserId);
entity.Property(e => e.ProductName)
.IsRequired()
.HasMaxLength(50);
entity.Property(e => e.ProductNumber)
.IsRequired()
.HasMaxLength(30);
entity.HasOne(d => d.User)
.WithMany(p => p.Products)
.HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<TagProduct>(entity =>
{
entity.HasKey(e => new { e.TagId, e.ProductProductId });
entity.HasIndex(e => e.ProductProductId);
entity.HasIndex(e => e.TagId);
entity.Property(e => e.TagId);
entity.Property(e => e.ProductProductId);
entity.HasOne(d => d.Product)
.WithMany(p => p.TagProducts)
.HasForeignKey(d => d.ProductProductId);
entity.HasOne(d => d.Tag)
.WithMany(p => p.TagProducts)
.HasForeignKey(d => d.TagId);
});
modelBuilder.Entity<User>(entity =>
{
entity.Property(e => e.Name).IsRequired();
});
}
public override int SaveChanges()
{
var changedEntityNames = this.GetChangedEntityNames();
this.ChangeTracker.AutoDetectChangesEnabled = false; // for performance reasons, to avoid calling DetectChanges() again.
var result = base.SaveChanges();
this.ChangeTracker.AutoDetectChangesEnabled = true;
this.GetService<IEFCacheServiceProvider>().InvalidateCacheDependencies(changedEntityNames);
return result;
}
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
var changedEntityNames = this.GetChangedEntityNames();
this.ChangeTracker.AutoDetectChangesEnabled = false; // for performance reasons, to avoid calling DetectChanges() again.
var result = base.SaveChangesAsync(cancellationToken);
this.ChangeTracker.AutoDetectChangesEnabled = true;
this.GetService<IEFCacheServiceProvider>().InvalidateCacheDependencies(changedEntityNames);
return result;
}
}
}