Injecting ApplicationUserManager in AccountController

Injecting IConfiguration to MusicStoreContext
This commit is contained in:
Praburaj 2014-05-01 16:47:01 -07:00
parent 3ae8b93e26
commit 65d4057132
4 changed files with 15 additions and 16 deletions

View File

@ -1,5 +1,4 @@
using Microsoft.AspNet.DependencyInjection; using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Security; using Microsoft.AspNet.Identity.Security;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding;
@ -12,11 +11,13 @@ namespace MusicStore.Controllers
[Authorize] [Authorize]
public class AccountController : Controller public class AccountController : Controller
{ {
public UserManager<ApplicationUser> UserManager public AccountController(ApplicationUserManager userManager)
{ {
get { return Context.ApplicationServices.GetService<ApplicationUserManager>(); } UserManager = userManager;
} }
public UserManager<ApplicationUser> UserManager { get; private set; }
private SignInManager<ApplicationUser> _signInManager; private SignInManager<ApplicationUser> _signInManager;
public SignInManager<ApplicationUser> SignInManager public SignInManager<ApplicationUser> SignInManager
{ {

View File

@ -1,21 +1,20 @@
using System; using Microsoft.AspNet.ConfigurationModel;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.InMemory; using Microsoft.Data.InMemory;
using Microsoft.Data.SqlServer; using Microsoft.Data.SqlServer;
using Microsoft.AspNet.DependencyInjection; using System;
using Microsoft.AspNet.ConfigurationModel;
namespace MusicStore.Models namespace MusicStore.Models
{ {
public class MusicStoreContext : DbContext public class MusicStoreContext : DbContext
{ {
private readonly IServiceProvider _serviceProvider; private readonly IConfiguration _configuration;
public MusicStoreContext(IServiceProvider serviceProvider) public MusicStoreContext(IServiceProvider serviceProvider, IConfiguration configuration)
: base(serviceProvider) : base(serviceProvider)
{ {
_serviceProvider = serviceProvider; _configuration = configuration;
} }
public DbSet<Album> Albums { get; set; } public DbSet<Album> Albums { get; set; }
@ -27,9 +26,8 @@ namespace MusicStore.Models
protected override void OnConfiguring(EntityConfigurationBuilder builder) protected override void OnConfiguring(EntityConfigurationBuilder builder)
{ {
var configuration = _serviceProvider.GetService<IConfiguration>();
#if NET45 #if NET45
builder.SqlServerConnectionString(configuration.Get("Data:DefaultConnection:ConnectionString")); builder.SqlServerConnectionString(_configuration.Get("Data:DefaultConnection:ConnectionString"));
#else #else
builder.UseInMemoryStore(persist: true); builder.UseInMemoryStore(persist: true);
#endif #endif

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.SqlServer; using Microsoft.Data.SqlServer;
using Microsoft.AspNet.DependencyInjection;
namespace MusicStore.Models namespace MusicStore.Models
{ {
@ -13,7 +14,7 @@ namespace MusicStore.Models
public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider) public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider)
{ {
using (var db = new MusicStoreContext(serviceProvider)) using (var db = serviceProvider.GetService<MusicStoreContext>())
{ {
var sqlServerDataStore = db.Configuration.DataStore as SqlServerDataStore; var sqlServerDataStore = db.Configuration.DataStore as SqlServerDataStore;
if (sqlServerDataStore != null) if (sqlServerDataStore != null)
@ -48,12 +49,12 @@ namespace MusicStore.Models
{ {
// Query in a separate context so that we can attach existing entities as modified // Query in a separate context so that we can attach existing entities as modified
List<TEntity> existingData; List<TEntity> existingData;
using (var db = new MusicStoreContext(serviceProvider)) using (var db = serviceProvider.GetService<MusicStoreContext>())
{ {
existingData = db.Set<TEntity>().ToList(); existingData = db.Set<TEntity>().ToList();
} }
using (var db = new MusicStoreContext(serviceProvider)) using (var db = serviceProvider.GetService<MusicStoreContext>())
{ {
foreach (var item in entities) foreach (var item in entities)
{ {

View File

@ -1 +0,0 @@
\\indigofs\commonshare\prabht\k\LKGTools\LkgProjectGenerator.exe "%CD%\..\..\Packages"