Merge pull request #261 from aspnet/UpdateDbContext
Changes to use AddDbContext method
This commit is contained in:
commit
41e8d22d3a
|
|
@ -49,31 +49,22 @@ namespace MusicStore
|
||||||
if (runningOnMono)
|
if (runningOnMono)
|
||||||
{
|
{
|
||||||
services.AddEntityFramework()
|
services.AddEntityFramework()
|
||||||
.AddInMemoryStore();
|
.AddInMemoryStore()
|
||||||
|
.AddDbContext<MusicStoreContext>(options =>
|
||||||
|
{
|
||||||
|
options.UseInMemoryStore();
|
||||||
|
}); ;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
services.AddEntityFramework()
|
services.AddEntityFramework()
|
||||||
.AddSqlServer();
|
.AddSqlServer()
|
||||||
|
.AddDbContext<MusicStoreContext>(options =>
|
||||||
|
{
|
||||||
|
options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString"));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
services.AddScoped<MusicStoreContext>();
|
|
||||||
|
|
||||||
// Configure DbContext
|
|
||||||
services.Configure<MusicStoreDbContextOptions>(options =>
|
|
||||||
{
|
|
||||||
options.DefaultAdminUserName = configuration.Get("DefaultAdminUsername");
|
|
||||||
options.DefaultAdminPassword = configuration.Get("DefaultAdminPassword");
|
|
||||||
if (runningOnMono)
|
|
||||||
{
|
|
||||||
options.UseInMemoryStore();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add Identity services to the services container
|
// Add Identity services to the services container
|
||||||
services.AddDefaultIdentity<MusicStoreContext, ApplicationUser, IdentityRole>(configuration);
|
services.AddDefaultIdentity<MusicStoreContext, ApplicationUser, IdentityRole>(configuration);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
using System;
|
using Microsoft.AspNet.Identity;
|
||||||
using Microsoft.AspNet.Identity;
|
|
||||||
using Microsoft.AspNet.Identity.EntityFramework;
|
using Microsoft.AspNet.Identity.EntityFramework;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using Microsoft.Data.Entity.Metadata;
|
using Microsoft.Data.Entity.Metadata;
|
||||||
using Microsoft.Framework.OptionsModel;
|
|
||||||
|
|
||||||
namespace MusicStore.Models
|
namespace MusicStore.Models
|
||||||
{
|
{
|
||||||
|
|
@ -11,8 +9,7 @@ namespace MusicStore.Models
|
||||||
|
|
||||||
public class MusicStoreContext : IdentityDbContext<ApplicationUser>
|
public class MusicStoreContext : IdentityDbContext<ApplicationUser>
|
||||||
{
|
{
|
||||||
public MusicStoreContext(IServiceProvider serviceProvider, IOptions<MusicStoreDbContextOptions> optionsAccessor)
|
public MusicStoreContext()
|
||||||
: base(serviceProvider, optionsAccessor.Options)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -41,10 +38,4 @@ namespace MusicStore.Models
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MusicStoreDbContextOptions : DbContextOptions
|
|
||||||
{
|
|
||||||
public string DefaultAdminUserName { get; set; }
|
|
||||||
|
|
||||||
public string DefaultAdminPassword { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -7,13 +7,15 @@ using Microsoft.AspNet.Identity;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using Microsoft.Data.Entity.SqlServer;
|
using Microsoft.Data.Entity.SqlServer;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Framework.OptionsModel;
|
using Microsoft.Framework.ConfigurationModel;
|
||||||
|
|
||||||
namespace MusicStore.Models
|
namespace MusicStore.Models
|
||||||
{
|
{
|
||||||
public static class SampleData
|
public static class SampleData
|
||||||
{
|
{
|
||||||
const string imgUrl = "~/Images/placeholder.png";
|
const string imgUrl = "~/Images/placeholder.png";
|
||||||
|
const string defaultAdminUserName = "DefaultAdminUserName";
|
||||||
|
const string defaultAdminPassword = "defaultAdminPassword";
|
||||||
|
|
||||||
public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider)
|
public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
|
|
@ -78,7 +80,10 @@ namespace MusicStore.Models
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private static async Task CreateAdminUser(IServiceProvider serviceProvider)
|
private static async Task CreateAdminUser(IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
var options = serviceProvider.GetService<IOptions<MusicStoreDbContextOptions>>().Options;
|
var configuration = new Configuration()
|
||||||
|
.AddJsonFile("config.json")
|
||||||
|
.AddEnvironmentVariables();
|
||||||
|
|
||||||
//const string adminRole = "Administrator";
|
//const string adminRole = "Administrator";
|
||||||
|
|
||||||
var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
|
var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
|
||||||
|
|
@ -89,11 +94,11 @@ namespace MusicStore.Models
|
||||||
// await roleManager.CreateAsync(new IdentityRole(adminRole));
|
// await roleManager.CreateAsync(new IdentityRole(adminRole));
|
||||||
//}
|
//}
|
||||||
|
|
||||||
var user = await userManager.FindByNameAsync(options.DefaultAdminUserName);
|
var user = await userManager.FindByNameAsync(configuration.Get<string>(defaultAdminUserName));
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
user = new ApplicationUser { UserName = options.DefaultAdminUserName };
|
user = new ApplicationUser { UserName = configuration.Get<string>(defaultAdminUserName) };
|
||||||
await userManager.CreateAsync(user, options.DefaultAdminPassword);
|
await userManager.CreateAsync(user, configuration.Get<string>(defaultAdminPassword));
|
||||||
//await userManager.AddToRoleAsync(user, adminRole);
|
//await userManager.AddToRoleAsync(user, adminRole);
|
||||||
await userManager.AddClaimAsync(user, new Claim("ManageStore", "Allowed"));
|
await userManager.AddClaimAsync(user, new Claim("ManageStore", "Allowed"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,31 +33,22 @@ namespace MusicStore
|
||||||
if (runningOnMono)
|
if (runningOnMono)
|
||||||
{
|
{
|
||||||
services.AddEntityFramework()
|
services.AddEntityFramework()
|
||||||
.AddInMemoryStore();
|
.AddInMemoryStore()
|
||||||
|
.AddDbContext<MusicStoreContext>(options =>
|
||||||
|
{
|
||||||
|
options.UseInMemoryStore();
|
||||||
|
}); ;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
services.AddEntityFramework()
|
services.AddEntityFramework()
|
||||||
.AddSqlServer();
|
.AddSqlServer()
|
||||||
|
.AddDbContext<MusicStoreContext>(options =>
|
||||||
|
{
|
||||||
|
options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
services.AddScoped<MusicStoreContext>();
|
|
||||||
|
|
||||||
// Configure DbContext
|
|
||||||
services.Configure<MusicStoreDbContextOptions>(options =>
|
|
||||||
{
|
|
||||||
options.DefaultAdminUserName = Configuration.Get("DefaultAdminUsername");
|
|
||||||
options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword");
|
|
||||||
if (runningOnMono)
|
|
||||||
{
|
|
||||||
options.UseInMemoryStore();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add Identity services to the services container
|
// Add Identity services to the services container
|
||||||
services.AddDefaultIdentity<MusicStoreContext, ApplicationUser, IdentityRole>(Configuration);
|
services.AddDefaultIdentity<MusicStoreContext, ApplicationUser, IdentityRole>(Configuration);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -70,17 +70,11 @@ namespace MusicStore
|
||||||
{
|
{
|
||||||
// Add EF services to the services container
|
// Add EF services to the services container
|
||||||
services.AddEntityFramework()
|
services.AddEntityFramework()
|
||||||
.AddSqlServer();
|
.AddSqlServer()
|
||||||
|
.AddDbContext<MusicStoreContext>(options =>
|
||||||
services.AddScoped<MusicStoreContext>();
|
{
|
||||||
|
options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString"));
|
||||||
// Configure DbContext
|
});
|
||||||
services.Configure<MusicStoreDbContextOptions>(options =>
|
|
||||||
{
|
|
||||||
options.DefaultAdminUserName = configuration.Get("DefaultAdminUsername");
|
|
||||||
options.DefaultAdminPassword = configuration.Get("DefaultAdminPassword");
|
|
||||||
options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString"));
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add Identity services to the services container
|
// Add Identity services to the services container
|
||||||
services.AddDefaultIdentity<MusicStoreContext, ApplicationUser, IdentityRole>(configuration);
|
services.AddDefaultIdentity<MusicStoreContext, ApplicationUser, IdentityRole>(configuration);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue