Changes to use AddDbContext method
This commit is contained in:
parent
0c7b714680
commit
7dd78bbfef
|
|
@ -49,31 +49,22 @@ namespace MusicStore
|
|||
if (runningOnMono)
|
||||
{
|
||||
services.AddEntityFramework()
|
||||
.AddInMemoryStore();
|
||||
.AddInMemoryStore()
|
||||
.AddDbContext<MusicStoreContext>(options =>
|
||||
{
|
||||
options.UseInMemoryStore();
|
||||
}); ;
|
||||
}
|
||||
else
|
||||
{
|
||||
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
|
||||
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.Data.Entity;
|
||||
using Microsoft.Data.Entity.Metadata;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace MusicStore.Models
|
||||
{
|
||||
|
|
@ -11,8 +9,7 @@ namespace MusicStore.Models
|
|||
|
||||
public class MusicStoreContext : IdentityDbContext<ApplicationUser>
|
||||
{
|
||||
public MusicStoreContext(IServiceProvider serviceProvider, IOptions<MusicStoreDbContextOptions> optionsAccessor)
|
||||
: base(serviceProvider, optionsAccessor.Options)
|
||||
public MusicStoreContext()
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -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.SqlServer;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
|
||||
namespace MusicStore.Models
|
||||
{
|
||||
public static class SampleData
|
||||
{
|
||||
const string imgUrl = "~/Images/placeholder.png";
|
||||
const string defaultAdminUserName = "DefaultAdminUserName";
|
||||
const string defaultAdminPassword = "defaultAdminPassword";
|
||||
|
||||
public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider)
|
||||
{
|
||||
|
|
@ -78,7 +80,10 @@ namespace MusicStore.Models
|
|||
/// <returns></returns>
|
||||
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";
|
||||
|
||||
var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
|
||||
|
|
@ -89,11 +94,11 @@ namespace MusicStore.Models
|
|||
// 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)
|
||||
{
|
||||
user = new ApplicationUser { UserName = options.DefaultAdminUserName };
|
||||
await userManager.CreateAsync(user, options.DefaultAdminPassword);
|
||||
user = new ApplicationUser { UserName = configuration.Get<string>(defaultAdminUserName) };
|
||||
await userManager.CreateAsync(user, configuration.Get<string>(defaultAdminPassword));
|
||||
//await userManager.AddToRoleAsync(user, adminRole);
|
||||
await userManager.AddClaimAsync(user, new Claim("ManageStore", "Allowed"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,31 +33,22 @@ namespace MusicStore
|
|||
if (runningOnMono)
|
||||
{
|
||||
services.AddEntityFramework()
|
||||
.AddInMemoryStore();
|
||||
.AddInMemoryStore()
|
||||
.AddDbContext<MusicStoreContext>(options =>
|
||||
{
|
||||
options.UseInMemoryStore();
|
||||
}); ;
|
||||
}
|
||||
else
|
||||
{
|
||||
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
|
||||
services.AddDefaultIdentity<MusicStoreContext, ApplicationUser, IdentityRole>(Configuration);
|
||||
|
||||
|
|
|
|||
|
|
@ -70,17 +70,11 @@ namespace MusicStore
|
|||
{
|
||||
// Add EF services to the services container
|
||||
services.AddEntityFramework()
|
||||
.AddSqlServer();
|
||||
|
||||
services.AddScoped<MusicStoreContext>();
|
||||
|
||||
// Configure DbContext
|
||||
services.Configure<MusicStoreDbContextOptions>(options =>
|
||||
{
|
||||
options.DefaultAdminUserName = configuration.Get("DefaultAdminUsername");
|
||||
options.DefaultAdminPassword = configuration.Get("DefaultAdminPassword");
|
||||
options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString"));
|
||||
});
|
||||
.AddSqlServer()
|
||||
.AddDbContext<MusicStoreContext>(options =>
|
||||
{
|
||||
options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString"));
|
||||
});
|
||||
|
||||
// Add Identity services to the services container
|
||||
services.AddDefaultIdentity<MusicStoreContext, ApplicationUser, IdentityRole>(configuration);
|
||||
|
|
|
|||
Loading…
Reference in New Issue