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 3339482
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:29:50+00:00 2026-05-18T00:29:50+00:00

We have a site using Unity and IUnitOfWork for our EF context. Until now

  • 0

We have a site using Unity and IUnitOfWork for our EF context. Until now we’ve only been using a single EF Context so this is the one mapped in Unity config. This has all been handled through constructor injection and this is something we’d like to maintain for consistency.

We’ve now introduced another EF Context for our PaymentController that is used within the site but Unity config currently only allows us to create one type for IUnitOfWork.

I know that I can create a new <register/> element for the new context with a distinct name attribute but how do I implement this within the controller constructor to use the one named payments?

  <register type="IUnitOfWork" mapTo="FirstContext" />
  <register type="IUnitOfWork" mapTo="PaymentsContext" name="payments"/>

  public class PaymentController()
  {
    public PaymentController(IUnitOfWork unitOfWork) 
    {
        //How to I tell unity that this needs to be a payments
        _unitOfWork = unitOfWork;
    }
  }

Many Thanks

  • 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-05-18T00:29:51+00:00Added an answer on May 18, 2026 at 12:29 am

    When the FirstContext and PaymentsContext have each an unique set of entities (for instance, each connect to a different database) it is worth wild to explicitly define this in code. One way of doing this is by specifying a factory for each unit of work:

    public interface IFirstContextFactory
    {
        IUnitOfWork CreateNew();
    }
    
    public interface IPaymentContextFactory
    {
        IUnitOfWork CreateNew();
    }
    
    public class PaymentController()
    {
       public PaymentController(PaymentContextFactory paymentContextFactory)
       {
          //How to I tell unity that this needs to be a payments
          this.paymentContextFactory = paymentContextFactory;
       }
    
       public void DoSomething()
       {
          using (var context = this.paymentContextFactory.CreateNew())
          {
             // Do something useful
    
              context.Commit();
          }
       }
    }
    

    Not only makes this your dependencies very clear (because you know what type of context the code is dealing with), but it also simplifies the DI configuration, because you won’t need any named registrations.

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

Sidebar

Related Questions

I've been developing a site using ASP.NET MVC, and have decided to use the
Since I have started using this site, I keep hearing about the Boost library.
I have a site using a custom favicon.ico. The favicon displays as expected in
I have a web site using apache httpd as the server and mysql as
I have a site that is using x509 client certificates (2 way SSL) to
I have a simple Tray icon program that opens a site using System.Diagnostics.Process.Start(URL) And
I am currently creating an e-commerce site using C# ASP.NET MVC and have just
I have a site that I am currently working on in ASP.NET 2.0 using
I have a site developed in ASP.NET and hosted on a windows server using
I have a DotNetNuke site where my main logo is a PNG file using

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.