-
Notifications
You must be signed in to change notification settings - Fork 5
/
GenerateUserFriendlyIdDbContext.cs
50 lines (46 loc) · 2.89 KB
/
GenerateUserFriendlyIdDbContext.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
using DevExpress.ExpressApp.EFCore.Updating;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using DevExpress.Persistent.BaseImpl.EF.PermissionPolicy;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.ExpressApp.Design;
using DevExpress.ExpressApp.EFCore.DesignTime;
using Demo.Module.BusinessObjects;
namespace GenerateUserFriendlyId.Module.BusinessObjects;
// This code allows our Model Editor to get relevant EF Core metadata at design time.
// For details, please refer to https://supportcenter.devexpress.com/ticket/details/t933891.
public class GenerateUserFriendlyIdContextInitializer : DbContextTypesInfoInitializerBase {
protected override DbContext CreateDbContext() {
var optionsBuilder = new DbContextOptionsBuilder<GenerateUserFriendlyIdEFCoreDbContext>()
.UseSqlServer(";")
.UseChangeTrackingProxies()
.UseObjectSpaceLinkProxies();
return new GenerateUserFriendlyIdEFCoreDbContext(optionsBuilder.Options);
}
}
//This factory creates DbContext for design-time services. For example, it is required for database migration.
public class GenerateUserFriendlyIdDesignTimeDbContextFactory : IDesignTimeDbContextFactory<GenerateUserFriendlyIdEFCoreDbContext> {
public GenerateUserFriendlyIdEFCoreDbContext CreateDbContext(string[] args) {
throw new InvalidOperationException("Make sure that the database connection string and connection provider are correct. After that, uncomment the code below and remove this exception.");
//var optionsBuilder = new DbContextOptionsBuilder<GenerateUserFriendlyIdEFCoreDbContext>();
//optionsBuilder.UseSqlServer("Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=E2829EFCore");
//optionsBuilder.UseChangeTrackingProxies();
//optionsBuilder.UseObjectSpaceLinkProxies();
//return new GenerateUserFriendlyIdEFCoreDbContext(optionsBuilder.Options);
}
}
[TypesInfoInitializer(typeof(GenerateUserFriendlyIdContextInitializer))]
public class GenerateUserFriendlyIdEFCoreDbContext : DbContext {
public GenerateUserFriendlyIdEFCoreDbContext(DbContextOptions<GenerateUserFriendlyIdEFCoreDbContext> options) : base(options) {
}
public DbSet<Address> Addresses { get; set; }
public DbSet<Contact> Contacts { get; set; }
public DbSet<Document> Documents { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.HasChangeTrackingStrategy(ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues);
modelBuilder.Entity<Address>().UsePropertyAccessMode(PropertyAccessMode.FieldDuringConstruction);
modelBuilder.Entity<Contact>().UsePropertyAccessMode(PropertyAccessMode.FieldDuringConstruction);
modelBuilder.Entity<Document>().UsePropertyAccessMode(PropertyAccessMode.FieldDuringConstruction);
}
}