Add application DbContext
This commit is contained in:
parent
767a857635
commit
d159cb9b00
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,6 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
|
|
@ -12,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
|||
|
||||
namespace Company.WebApplication1.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(IdentityDbContext))]
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("00000000000000_CreateIdentitySchema")]
|
||||
partial class CreateIdentitySchema
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
|
|
@ -12,8 +11,8 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
|||
|
||||
namespace Company.WebApplication1.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(IdentityDbContext))]
|
||||
partial class IdentityDbContextModelSnapshot : ModelSnapshot
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
|
|
@ -13,7 +13,6 @@ using Microsoft.AspNetCore.Authorization;
|
|||
using Microsoft.AspNetCore.Builder;
|
||||
#if (IndividualLocalAuth)
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
#endif
|
||||
#if (OrganizationalAuth || IndividualAuth)
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
|
@ -26,6 +25,7 @@ using Microsoft.AspNetCore.Mvc.Authorization;
|
|||
#endif
|
||||
#if (IndividualLocalAuth)
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Company.WebApplication1.Data;
|
||||
#endif
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
|
@ -57,20 +57,16 @@ namespace Company.WebApplication1
|
|||
});
|
||||
|
||||
#if (IndividualLocalAuth)
|
||||
services.AddDbContext<IdentityDbContext>(options =>
|
||||
#if (UseLocalDB)
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
#if (UseLocalDB)
|
||||
options.UseSqlServer(
|
||||
Configuration.GetConnectionString("DefaultConnection"),
|
||||
sqlOptions => sqlOptions.MigrationsAssembly("Company.WebApplication1")
|
||||
));
|
||||
#else
|
||||
Configuration.GetConnectionString("DefaultConnection")));
|
||||
#else
|
||||
options.UseSqlite(
|
||||
Configuration.GetConnectionString("DefaultConnection"),
|
||||
sqlOptions => sqlOptions.MigrationsAssembly("Company.WebApplication1")
|
||||
));
|
||||
#endif
|
||||
Configuration.GetConnectionString("DefaultConnection")));
|
||||
#endif
|
||||
services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
|
||||
.AddEntityFrameworkStores<IdentityDbContext>()
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddDefaultUI()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
|
|
@ -80,24 +76,15 @@ namespace Company.WebApplication1
|
|||
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
||||
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
|
||||
})
|
||||
#if (OrganizationalAuth)
|
||||
#if (OrganizationalAuth)
|
||||
.AddAzureAd(options => Configuration.Bind("AzureAd", options))
|
||||
#elif (IndividualB2CAuth)
|
||||
#elif (IndividualB2CAuth)
|
||||
.AddAzureAdB2C(options => Configuration.Bind("AzureAdB2C", options))
|
||||
#endif
|
||||
#endif
|
||||
.AddCookie();
|
||||
|
||||
#endif
|
||||
#if (IndividualLocalAuth)
|
||||
services.AddMvc()
|
||||
.AddRazorPagesOptions(options =>
|
||||
{
|
||||
options.Conventions.AuthorizeFolder("/Account/Manage");
|
||||
options.Conventions.AuthorizePage("/Account/Logout");
|
||||
})
|
||||
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
|
||||
|
||||
#elif (OrganizationalAuth)
|
||||
#if (OrganizationalAuth)
|
||||
services.AddMvc(options =>
|
||||
{
|
||||
var policy = new AuthorizationPolicyBuilder()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,6 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
|
|
@ -12,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
|||
|
||||
namespace Company.WebApplication1.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(IdentityDbContext))]
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("00000000000000_CreateIdentitySchema")]
|
||||
partial class CreateIdentitySchema
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
|
|
@ -12,8 +11,8 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
|||
|
||||
namespace Company.WebApplication1.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(IdentityDbContext))]
|
||||
partial class IdentityDbContextModelSnapshot : ModelSnapshot
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
|
|
@ -10,13 +10,13 @@ using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
|||
using Microsoft.AspNetCore.Builder;
|
||||
#if (IndividualLocalAuth)
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
#endif
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
#if (IndividualLocalAuth)
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Company.WebApplication1.Data;
|
||||
#endif
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
|
@ -42,21 +42,17 @@ namespace Company.WebApplication1
|
|||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
#if (IndividualLocalAuth)
|
||||
services.AddDbContext<IdentityDbContext>(options =>
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
#if (UseLocalDB)
|
||||
options.UseSqlServer(
|
||||
Configuration.GetConnectionString("DefaultConnection"),
|
||||
sqlOptions => sqlOptions.MigrationsAssembly("Company.WebApplication1")
|
||||
));
|
||||
Configuration.GetConnectionString("DefaultConnection")));
|
||||
#else
|
||||
options.UseSqlite(
|
||||
Configuration.GetConnectionString("DefaultConnection"),
|
||||
sqlOptions => sqlOptions.MigrationsAssembly("Company.WebApplication1")
|
||||
));
|
||||
Configuration.GetConnectionString("DefaultConnection")));
|
||||
#endif
|
||||
|
||||
services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
|
||||
.AddEntityFrameworkStores<IdentityDbContext>()
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddDefaultUI()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue