Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8496443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:51:25+00:00 2026-06-10T23:51:25+00:00

I have this really basic code in a MVC controller action. It maps an

  • 0

I have this really basic code in a MVC controller action. It maps an Operation model class to a very basic OperationVM view-model class .

public class OperationVM: Operation 
{
    public CategoryVM CategoryVM { get; set; }
}

I need to load the complete list of categories in order to create a CategoryVM instance.
Here’s how I (try to) create a List<OperationVM> to show in the view.

public class OperationsController : Controller {

    private SomeContext context = new SomeContext ();

    public ViewResult Index()
    {
        var ops = context.Operations.Include("blah...").ToList();
        Mapper.CreateMap<Operation, OperationVM>()
            .ForMember(
                dest => dest.CategoryVM, 
                opt => opt.MapFrom(
                    src => CreateCatVM(src.Category, context.Categories)
                    //  trouble here ----------------^^^^^^^
                )
            );
        var opVMs = ops.Select(op => Mapper.Map<Operation, OperationVM>(op))
                       .ToList();

        return View(opVMs);
    }
}

All works great first time I hit the page. The problem is, the mapper object is static. So when calling Mapper.CreateMap(), the instance of the current DbContext is saved in the closure given to CreateMap().

The 2nd time I hit the page, the static map is already in place, still using the reference to the initial, now disposed, DbContext.

The exact error is:

The operation cannot be completed because the DbContext has been disposed.

The question is: How can I make AutoMapper always use the current context instead of the initial one?

Is there a way to use an “instance” of automapper instead of the static Mapper class?
If this is possible, is it recommended to re-create the mapping every time? I’m worried about reflection slow-downs.

I read a bit about custom resolvers, but I get a similar problem – How do I get the custom resolver to use the current context?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:51:27+00:00Added an answer on June 10, 2026 at 11:51 pm

    It is possible, but the setup is a bit complicated. I use this in my projects with help of Ninject for dependency injection.

    AutoMapper has concept of TypeConverters. Converters provide a way to implement complex operations required to convert certain types in a separate class. If converting Category to CategoryVM requires a database lookup you can implement that logic in custom TypeConverter class similar to this:

    using System;
    using AutoMapper;
    
    public class CategoryToCategoryVMConverter : 
            TypeConverter<Category, CategoryVM>
    {
        public CategoryToCategoryVMConverter(DbContext context)
        {
            this.Context = context;
        }
    
        private DbContext Context { get; set; }
    
        protected override CategoryVM ConvertCore(Category source)
        {
            // use this.Context to lookup whatever you need
            return CreateCatVM(source, this.Context.Categories);
        }
    }
    

    You then to configure AutoMapper to use your converter:

    Mapper.CreateMap<Category, CategoryVM>().ConvertUsing<CategoryToCategoryVMConverter>();
    

    Here comes the tricky part. AutoMapper will need to create a new instance of our converter every time you map values, and it will need to provide DbContext instance for constructor. In my projects I use Ninject for dependency injection, and it is configured to use the same instance of DbContext while processing a request. This way the same instance of DbContext is injected both in your controller and in your AutoMapper converter. The trivial Ninject configuration would look like this:

    Bind<DbContext>().To<SomeContext>().InRequestScope();
    

    You can of course use some sort of factory pattern to get instance of DbContext instead of injecting it in constructors.

    Let me know if you have any questions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code, it looks alright and is really basic, but i can't
This is a really basic question but... I have some code like this var
hi i have this really basic class here .. and i want to use
this is a really basic question - I have a model called Book, which
So I have this pretty basic code in my document class: package { import
I realize this question is pretty basic, but I'm really stuck. I have a
this seems to be weird but it really happened. I have this code working
I have this in my Global.asax.cs: routes.MapRoute( User, User/{username}/{action}, new { controller = User,
I have an ASP.NET MVC web app which has a really basic subscription system
This is a really basic question, but what is wrong with this code? for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.