Skip to content

Commit

Permalink
add WebContext & DbContextConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
alimon808 committed Nov 2, 2017
1 parent 45e15f1 commit c868b68
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
33 changes: 33 additions & 0 deletions ContosoUniversity.Data/DbContexts/DbContextConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using ContosoUniversity.Data.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace ContosoUniversity.Data.DbContexts
{
public class DbContextConfig
{
public void ApplicationContextConfig(ModelBuilder modelBuilder, string schema = "")
{
modelBuilder.Entity<Course>().ToTable("Course", schema);
modelBuilder.Entity<Enrollment>().ToTable("Enrollment", schema);
modelBuilder.Entity<Student>().ToTable("Student", schema);
modelBuilder.Entity<Department>().ToTable("Department", schema);
modelBuilder.Entity<Instructor>().ToTable("Instructor", schema);
modelBuilder.Entity<OfficeAssignment>().ToTable("OfficeAssignment", schema);
modelBuilder.Entity<CourseAssignment>().ToTable("CourseAssignment", schema);
modelBuilder.Entity<Person>().ToTable("Person", schema);
modelBuilder.Entity<CourseAssignment>().HasKey(c => new { c.CourseID, c.InstructorID });
}

public void SecureApplicationContextConfig(ModelBuilder modelBuilder, string schema = "")
{
modelBuilder.Entity<ApplicationUser>().ToTable("Users", schema);
modelBuilder.Entity<IdentityRole>().ToTable("Role", schema);
modelBuilder.Entity<IdentityUserClaim<string>>().ToTable("UserClaim", schema);
modelBuilder.Entity<IdentityUserRole<string>>().ToTable("UserRole", schema);
modelBuilder.Entity<IdentityUserLogin<string>>().ToTable("UserLogin", schema);
modelBuilder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaim", schema);
modelBuilder.Entity<IdentityUserToken<string>>().ToTable("UserToken", schema);
}
}
}
37 changes: 37 additions & 0 deletions ContosoUniversity.Data/DbContexts/WebContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using ContosoUniversity.Data.DbContexts;
using ContosoUniversity.Data.Entities;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace ContosoUniversity.Data
{
public class WebContext : IdentityDbContext<ApplicationUser>
{
public WebContext(DbContextOptions<WebContext> options) : base(options)
{
}

public DbSet<Course> Courses { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Student> Students { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Instructor> Instructors { get; set; }
public DbSet<OfficeAssignment> OfficeAssignments { get; set; }
public DbSet<CourseAssignment> CourseAssignments { get; set; }
public DbSet<Person> People { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
string schema = "Contoso";
if (OperatingSystem.IsMacOs())
{
schema = null;
}

var config = new DbContextConfig();
config.SecureApplicationContextConfig(modelBuilder, schema);
config.ApplicationContextConfig(modelBuilder, schema);
}
}
}

0 comments on commit c868b68

Please sign in to comment.