Skip to content

Commit

Permalink
Merge pull request #261 from aspnet/UpdateDbContext
Browse files Browse the repository at this point in the history
Changes to use AddDbContext method
  • Loading branch information
Suhas Joshi committed Oct 16, 2014
2 parents 0c7b714 + 7dd78bb commit 41e8d22
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 65 deletions.
29 changes: 10 additions & 19 deletions src/MusicStore/Mocks/StartupSocialTesting.cs
Expand Up @@ -49,31 +49,22 @@ public void Configure(IApplicationBuilder app)
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);
Expand Down
13 changes: 2 additions & 11 deletions src/MusicStore/Models/MusicStoreContext.cs
@@ -1,18 +1,15 @@
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
{
public class ApplicationUser : IdentityUser { }

public class MusicStoreContext : IdentityDbContext<ApplicationUser>
{
public MusicStoreContext(IServiceProvider serviceProvider, IOptions<MusicStoreDbContextOptions> optionsAccessor)
: base(serviceProvider, optionsAccessor.Options)
public MusicStoreContext()
{

}
Expand Down Expand Up @@ -41,10 +38,4 @@ protected override void OnModelCreating(ModelBuilder builder)
}
}

public class MusicStoreDbContextOptions : DbContextOptions
{
public string DefaultAdminUserName { get; set; }

public string DefaultAdminPassword { get; set; }
}
}
15 changes: 10 additions & 5 deletions src/MusicStore/Models/SampleData.cs
Expand Up @@ -7,13 +7,15 @@
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)
{
Expand Down Expand Up @@ -78,7 +80,10 @@ private static async Task InsertTestData(IServiceProvider serviceProvider)
/// <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>>();
Expand All @@ -89,11 +94,11 @@ private static async Task CreateAdminUser(IServiceProvider serviceProvider)
// 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"));
}
Expand Down
29 changes: 10 additions & 19 deletions src/MusicStore/Startup.cs
Expand Up @@ -33,31 +33,22 @@ public void ConfigureServices(IServiceCollection services)
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);

Expand Down
16 changes: 5 additions & 11 deletions src/MusicStore/StartupNtlmAuthentication.cs
Expand Up @@ -70,17 +70,11 @@ public void Configure(IApplicationBuilder app)
{
// 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);
Expand Down

0 comments on commit 41e8d22

Please sign in to comment.