0

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();
    }
}
Claudio Gareca
  • 109
  • 1
  • 11
  • the whole point of a Unit Of Work is that it's self contained; It is a Work process from context creation, item manipulation, database transaction, context destruction. Why would you need to reference something from within this cycle from somewhere else? – Claies Mar 04 '15 at 18:52
  • hi, because i want use the identity asp and some repository for my others Entities, for example every works ok until I try to use the other entities in my Account Controller. if i need get some currency and asig to my user entity i have the next error "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" – Claudio Gareca Mar 04 '15 at 18:56
  • The question is really unanswerable in this state. It is not possible to know what your Context or UnitOfWork look like or how to restructure them without any code. – Claies Mar 04 '15 at 19:00
  • I Update the content whit my class. – Claudio Gareca Mar 04 '15 at 19:12

1 Answers1

2

after a lot analyzing and understand, I finally find the solution, first i have to inject the same context to avoid inject a new instance of the Context. the solution is:

For<DbContext>().Use(()=>System.Web.HttpContext.Current.GetOwinContext().Get<ApplicationDbContext>());

before i was injecting and add a new instance of the DBContex.

 For<DbContext>().Use<ApplicationDbContext>(new ApplicationDbContext());
Claudio Gareca
  • 109
  • 1
  • 11