-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathComplexDialogEFDbContext.cs
47 lines (44 loc) · 2.51 KB
/
ComplexDialogEFDbContext.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
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 ComplexDialogSample.Module.BusinessObjects;
namespace ComplexDialogEF.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 ComplexDialogEFContextInitializer : DbContextTypesInfoInitializerBase {
protected override DbContext CreateDbContext() {
var optionsBuilder = new DbContextOptionsBuilder<ComplexDialogEFEFCoreDbContext>()
.UseSqlServer(";")
.UseChangeTrackingProxies()
.UseObjectSpaceLinkProxies();
return new ComplexDialogEFEFCoreDbContext(optionsBuilder.Options);
}
}
//This factory creates DbContext for design-time services. For example, it is required for database migration.
public class ComplexDialogEFDesignTimeDbContextFactory : IDesignTimeDbContextFactory<ComplexDialogEFEFCoreDbContext> {
public ComplexDialogEFEFCoreDbContext 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<ComplexDialogEFEFCoreDbContext>();
//optionsBuilder.UseSqlServer("Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=ComplexDialogEF");
//optionsBuilder.UseChangeTrackingProxies();
//optionsBuilder.UseObjectSpaceLinkProxies();
//return new ComplexDialogEFEFCoreDbContext(optionsBuilder.Options);
}
}
[TypesInfoInitializer(typeof(ComplexDialogEFContextInitializer))]
public class ComplexDialogEFEFCoreDbContext : DbContext {
public ComplexDialogEFEFCoreDbContext(DbContextOptions<ComplexDialogEFEFCoreDbContext> options) : base(options) {
}
public DbSet<Team> Teams { get; set; }
public DbSet<Office> Offices { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Service> Services { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.HasChangeTrackingStrategy(ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues);
}
}