Skip to content

Commit b8f5824

Browse files
committed
Merge pull request #1 from aspnet/ControllerUpdates
Everything but the kitchen async... (Updates to how Music Store controllers use data) Specifically: - Dispose contexts - Use async wherever possible - Stop using initializers (currently hard-coded to drop and recreate each run) - Some general cleanup - Stop using AttachDbFilename Not included here: - No major changes to app structure - No major changes to data model - No major changes to error handling, concurrency, etc.
2 parents 53268f3 + c20584b commit b8f5824

15 files changed

+4143
-958
lines changed

.nuget/packages.config

Lines changed: 0 additions & 4 deletions
This file was deleted.

MusicStore.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MusicStore.k10", "src\Music
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MusicStore.net45", "src\MusicStore\MusicStore.net45.csproj", "{9C8A2D1F-D430-46DF-8F00-39E378EC8FB2}"
1111
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{44621553-AA7D-4893-8834-79582A7D8348}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU

src/MvcMusicStore/App_Start/Startup.App.cs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,54 @@
1-
using Microsoft.AspNet.Identity;
1+
using System.Configuration;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNet.Identity;
24
using Microsoft.AspNet.Identity.EntityFramework;
3-
using Microsoft.Owin;
4-
using Microsoft.Owin.Security.Cookies;
55
using MvcMusicStore.Models;
66
using Owin;
7-
using System.Configuration;
8-
using System.Threading.Tasks;
97

108
namespace MvcMusicStore
119
{
1210
public partial class Startup
1311
{
12+
private const string RoleName = "Administrator";
13+
1414
public void ConfigureApp(IAppBuilder app)
1515
{
16-
System.Data.Entity.Database.SetInitializer(new MvcMusicStore.Models.SampleData());
16+
using (var context = new MusicStoreEntities())
17+
{
18+
context.Database.Delete();
19+
context.Database.Create();
20+
21+
new SampleData().Seed(context);
22+
}
1723

18-
CreateAdminUser();
24+
CreateAdminUser().Wait();
1925
}
2026

21-
private async void CreateAdminUser()
27+
private async Task CreateAdminUser()
2228
{
23-
string _username = ConfigurationManager.AppSettings["DefaultAdminUsername"];
24-
string _password = ConfigurationManager.AppSettings["DefaultAdminPassword"];
25-
string _role = "Administrator";
26-
27-
var context = new ApplicationDbContext();
28-
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
29-
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
29+
var username = ConfigurationManager.AppSettings["DefaultAdminUsername"];
30+
var password = ConfigurationManager.AppSettings["DefaultAdminPassword"];
3031

31-
var role = new IdentityRole(_role);
32-
var result = await roleManager.RoleExistsAsync(_role);
33-
if (result == false)
32+
using (var context = new ApplicationDbContext())
3433
{
35-
await roleManager.CreateAsync(role);
36-
}
34+
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
35+
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
3736

38-
var user = await userManager.FindByNameAsync(_username);
39-
if (user == null)
40-
{
41-
user = new ApplicationUser { UserName = _username };
42-
await userManager.CreateAsync(user, _password);
43-
await userManager.AddToRoleAsync(user.Id, _role);
37+
var role = new IdentityRole(RoleName);
38+
39+
var result = await roleManager.RoleExistsAsync(RoleName);
40+
if (!result)
41+
{
42+
await roleManager.CreateAsync(role);
43+
}
44+
45+
var user = await userManager.FindByNameAsync(username);
46+
if (user == null)
47+
{
48+
user = new ApplicationUser { UserName = username };
49+
await userManager.CreateAsync(user, password);
50+
await userManager.AddToRoleAsync(user.Id, RoleName);
51+
}
4452
}
4553
}
4654
}

0 commit comments

Comments
 (0)