Add application DbContext

This commit is contained in:
Javier Calvarro Nelson 2018-01-22 12:57:23 -08:00
parent 767a857635
commit d159cb9b00
8 changed files with 55 additions and 44 deletions

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace Company.WebApplication1.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}

View File

@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
#endif #endif
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata;
@ -12,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Company.WebApplication1.Data.Migrations namespace Company.WebApplication1.Data.Migrations
{ {
[DbContext(typeof(IdentityDbContext))] [DbContext(typeof(ApplicationDbContext))]
[Migration("00000000000000_CreateIdentitySchema")] [Migration("00000000000000_CreateIdentitySchema")]
partial class CreateIdentitySchema partial class CreateIdentitySchema
{ {

View File

@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
#endif #endif
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata;
@ -12,8 +11,8 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Company.WebApplication1.Data.Migrations namespace Company.WebApplication1.Data.Migrations
{ {
[DbContext(typeof(IdentityDbContext))] [DbContext(typeof(ApplicationDbContext))]
partial class IdentityDbContextModelSnapshot : ModelSnapshot partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{ {
protected override void BuildModel(ModelBuilder modelBuilder) protected override void BuildModel(ModelBuilder modelBuilder)
{ {

View File

@ -13,7 +13,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
#if (IndividualLocalAuth) #if (IndividualLocalAuth)
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
#endif #endif
#if (OrganizationalAuth || IndividualAuth) #if (OrganizationalAuth || IndividualAuth)
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -26,6 +25,7 @@ using Microsoft.AspNetCore.Mvc.Authorization;
#endif #endif
#if (IndividualLocalAuth) #if (IndividualLocalAuth)
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Company.WebApplication1.Data;
#endif #endif
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -57,20 +57,16 @@ namespace Company.WebApplication1
}); });
#if (IndividualLocalAuth) #if (IndividualLocalAuth)
services.AddDbContext<IdentityDbContext>(options => services.AddDbContext<ApplicationDbContext>(options =>
#if (UseLocalDB) #if (UseLocalDB)
options.UseSqlServer( options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection"), Configuration.GetConnectionString("DefaultConnection")));
sqlOptions => sqlOptions.MigrationsAssembly("Company.WebApplication1") #else
));
#else
options.UseSqlite( options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection"), Configuration.GetConnectionString("DefaultConnection")));
sqlOptions => sqlOptions.MigrationsAssembly("Company.WebApplication1") #endif
));
#endif
services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128) services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
.AddEntityFrameworkStores<IdentityDbContext>() .AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI() .AddDefaultUI()
.AddDefaultTokenProviders(); .AddDefaultTokenProviders();
@ -80,24 +76,15 @@ namespace Company.WebApplication1
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}) })
#if (OrganizationalAuth) #if (OrganizationalAuth)
.AddAzureAd(options => Configuration.Bind("AzureAd", options)) .AddAzureAd(options => Configuration.Bind("AzureAd", options))
#elif (IndividualB2CAuth) #elif (IndividualB2CAuth)
.AddAzureAdB2C(options => Configuration.Bind("AzureAdB2C", options)) .AddAzureAdB2C(options => Configuration.Bind("AzureAdB2C", options))
#endif #endif
.AddCookie(); .AddCookie();
#endif #endif
#if (IndividualLocalAuth) #if (OrganizationalAuth)
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizeFolder("/Account/Manage");
options.Conventions.AuthorizePage("/Account/Logout");
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
#elif (OrganizationalAuth)
services.AddMvc(options => services.AddMvc(options =>
{ {
var policy = new AuthorizationPolicyBuilder() var policy = new AuthorizationPolicyBuilder()

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace Company.WebApplication1.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}

View File

@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
#endif #endif
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata;
@ -12,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Company.WebApplication1.Data.Migrations namespace Company.WebApplication1.Data.Migrations
{ {
[DbContext(typeof(IdentityDbContext))] [DbContext(typeof(ApplicationDbContext))]
[Migration("00000000000000_CreateIdentitySchema")] [Migration("00000000000000_CreateIdentitySchema")]
partial class CreateIdentitySchema partial class CreateIdentitySchema
{ {

View File

@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
#endif #endif
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata;
@ -12,8 +11,8 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Company.WebApplication1.Data.Migrations namespace Company.WebApplication1.Data.Migrations
{ {
[DbContext(typeof(IdentityDbContext))] [DbContext(typeof(ApplicationDbContext))]
partial class IdentityDbContextModelSnapshot : ModelSnapshot partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{ {
protected override void BuildModel(ModelBuilder modelBuilder) protected override void BuildModel(ModelBuilder modelBuilder)
{ {

View File

@ -10,13 +10,13 @@ using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
#if (IndividualLocalAuth) #if (IndividualLocalAuth)
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
#endif #endif
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
#if (IndividualLocalAuth) #if (IndividualLocalAuth)
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Company.WebApplication1.Data;
#endif #endif
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -42,21 +42,17 @@ namespace Company.WebApplication1
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
#if (IndividualLocalAuth) #if (IndividualLocalAuth)
services.AddDbContext<IdentityDbContext>(options => services.AddDbContext<ApplicationDbContext>(options =>
#if (UseLocalDB) #if (UseLocalDB)
options.UseSqlServer( options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection"), Configuration.GetConnectionString("DefaultConnection")));
sqlOptions => sqlOptions.MigrationsAssembly("Company.WebApplication1")
));
#else #else
options.UseSqlite( options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection"), Configuration.GetConnectionString("DefaultConnection")));
sqlOptions => sqlOptions.MigrationsAssembly("Company.WebApplication1")
));
#endif #endif
services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128) services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
.AddEntityFrameworkStores<IdentityDbContext>() .AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI() .AddDefaultUI()
.AddDefaultTokenProviders(); .AddDefaultTokenProviders();