Hi i have mi project in MVC5, i am using Identity 2.0, commonRepository and Structuremap to inject dependencies, the problem is when I am in the controller AccountController, i have one Contex and when my UnitOfWork inject the repositories it create other Instance.
how I can inject or replace the context of the identity whit my context from my UnitOfWork.
Regards
Update
AccountController
public class AccountController : Controller
{
private readonly ApplicationSignInManager SignInManager;
private readonly ApplicationUserManager UserManager;
private readonly IAuthenticationManager AuthenticationManager;
// private readonly IUbicationDao _ubicationDao;
private readonly ICultureDao _cultureDao;
private readonly ICurrencyDao _currecieDao;
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, ICultureDao cultureDao, ICurrencyDao currecieDao, IAuthenticationManager authenticationManager)
{
UserManager = userManager;
SignInManager = signInManager;
// _ubicationDao = ubicationDao;
_cultureDao = cultureDao;
_currecieDao = currecieDao;
AuthenticationManager = authenticationManager;
}}
DefaultRegistry StructureMap
public class DefaultRegistry : Registry {
#region Constructors and Destructors
public static IList<string> Assemblies
{
get
{
return new List<string>
{
"Interfaz",
"Persistencia"
};
}
}
public static IList<Tuple<string, string>> ManuallyWired
{
get
{
return new List<Tuple<string, string>>()
{
Tuple.Create("IUserStore<ApplicationUser>", "UserStore<ApplicationUser>>"),
Tuple.Create("DbContext", "ApplicationDbContext"),
Tuple.Create("IAuthenticationManager", "HttpContext.Current.GetOwinContext().Authentication"),
};
}
}
public DefaultRegistry()
{
Scan(
scan =>
{
foreach (var assembly in Assemblies)
{
scan.Assembly(assembly);
}
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.With(new ControllerConvention());
});
For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser>>();
For<DbContext>().Use<ApplicationDbContext>(new ApplicationDbContext());
For<IAuthenticationManager>().Use(() => HttpContext.Current.GetOwinContext().Authentication);
//DAos
For<ICultureDao>().Use<CultureDao>();
For<ICurrencyDao>().Use<CurrencyDao>();
For<IUbicationDao>().Use<UbicationDao>();
For<IActivatorWrapper>().Use<ActivatorWrapper>();
For<IUnitOfWorkHelper>().Use<UnitOfWorkHelper>();
}
#endregion
}
UnitofWork
public class UnitOfWorkHelper : IUnitOfWorkHelper
{
private ApplicationDbContext _sessionContext;
public event EventHandler<ObjectCreatedEventArgs> ObjectCreated;
public IApplicationDbContext DBContext
{
get
{
if (_sessionContext == null)
{
_sessionContext = new ApplicationDbContext();
((IObjectContextAdapter)_sessionContext).ObjectContext.ObjectMaterialized += (sender, e) => OnObjectCreated(e.Entity);
}
return _sessionContext;
}
}
private void OnObjectCreated(object entity)
{
if (ObjectCreated != null)
ObjectCreated(this, new ObjectCreatedEventArgs(entity));
}
public void SaveChanges()
{
this.DBContext.SaveChanges();
}
public void RollBack()
{
if (_sessionContext != null)
_sessionContext.ChangeTracker.Entries()
.ToList()
.ForEach(entry => entry.State = EntityState.Unchanged);
}
public void Dispose()
{
if (_sessionContext != null)
_sessionContext.Dispose();
}
}