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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:07:27+00:00 2026-05-12T21:07:27+00:00

I am trying to use unity to automatically inject a datacontext on my repository

  • 0

I am trying to use unity to automatically inject a datacontext on my repository using a new instance each time.., my idea is the ensure that each time a new datacontext is injected

Currently its failing on creating the repository, i think it can’t resolve MyDataContext

Before creating a constructor on “the repository” (see below) to take in the DataContext on my repository everything worked but now its failing..

I currently have this setup in my unity container which i create in global.asax, i have also registered the type MyDataContext which is standard DataContext

        container = new UnityContainer();

        Container.RegisterType<MyDataContext, MyDataContext>()
            .RegisterType<IOfficeRepository, OfficeRepository>()
            .RegisterType<IOfficeService, OfficeService>();

basically i have a service that calls the repository like so

public class OfficeService : IOfficeService
{

    IOfficeRepository repository = null;

    public OfficeService(IOfficeRepository repository)
    {
        this.repository = repository;

        if (this.repository == null)
            throw new InvalidOperationException("Repository cannot be null");
    }

here is my repository

public class OfficeRepository : IOfficeRepository
{
    private MyDataContext db;

    public OfficeRepository (MyDataContext dataContext)
    {
        this.db = dataContext;
    }

EDIT

I almost forgot i am doing this to create the service

officeService = Bootstrapper.Container.Resolve<IOfficeService>();

EDIT – THE ERROR BEING GENERATED

 Resolution of the dependency failed, type = "MarkSmith.IOfficeService", name = "".
 Exception message is: The current build operation (build key Build 
 Key[MarkSmith.OfficeService, null]) failed: The parameter repository could not be 
 resolved when attempting to call constructor 
 MarkSmith.OfficeService(MarkSmith.IOfficeRepository repository). (Strategy type BuildPlanStrategy, index 3)

EDIT – REMOVING Constructor on repository works

It is something to do with the datacontext because if i remove the constrcutor on the repository that takes a DataContext then all works, but of course i need it to accept a DataContext to be able to inject a “NEW” datacontext each time

public class OfficeRepository : IOfficeRepository
{
    private MyDataContext db new MyDataContext(); // CHANGE

    //public OfficeRepository (MyDataContext dataContext)
    //{
        //this.db = dataContext;
    //}

EDIT – ACTUAL ERROR

After digging deeper i have found this error ….

The type MyDataContext has multiple constructors of length 2. 
Unable to disambiguate. (Strategy type DynamicMethodConstructorStrategy, index 0)
(Strategy type BuildPlanStrategy, index 3)

EDIT – TEST TO RESOLVE THE DATACONTEXT with 1 line of code

This also fails with the same error as above – multiple constructors

  MyDataContext test = Bootstrapper.Container.Resolve<MyDataContext >();

EDIT – ALL CONSTRUCTORS ON MY DATACONTEXT

These were created by an exernal util but all should be well..

    [System.Diagnostics.DebuggerNonUserCode]
    public MyDataContext()
        : base(ConnectionString, mappingCache)
    {
        OnCreated();
    }

    [System.Diagnostics.DebuggerNonUserCode]
    public MyDataContext(string connection)
        : base(connection, mappingCache)
    {
        OnCreated();
    }

    [System.Diagnostics.DebuggerNonUserCode]
    public MyDataContext(System.Data.IDbConnection connection)
        : base(connection, mappingCache)
    {
        OnCreated();
    }

    [System.Diagnostics.DebuggerNonUserCode]
    public MyDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource)
        : base(connection, mappingSource)
    {
        OnCreated();
    }

    [System.Diagnostics.DebuggerNonUserCode]
    public MyDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource)
        : base(connection, mappingSource)
    {
        OnCreated();
    }

EDIT – To demonstrate creating the DataContext in code without Unity works 100% without issue

   MyDataContext tes2t = new MyDataContext ();
  • 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-12T21:07:28+00:00Added an answer on May 12, 2026 at 9:07 pm

    I’m not sure this works, but have you tried to register MyDataContext as a component rather than a type mapping?

    container.RegisterType<MyDataContext>();
    

    instead of

    container.RegisterType<MyDataContext, MyDataContext>();
    

    EDIT based on new information

    The culprit seems to be that MyDataContext has more than one constructor. This is a common issue with most DI Containers, because they need to pick and use only one. If you can remove the ambiguity by constraining MyDataContext to have only one constructor, that will probably be the simplest solution.

    Otherwise, you should be able to use an InjectionConstructor instance to identify the constructor when you register the repository. Let’s assume you want to use a constructor that takes a connection string as an argument:

    string connectionString =
        ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
    var injectedConnectionString = new InjectionConstructor(connectionString);
    container.RegisterType<MyDataContext>(injectedConnectionString);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to use Unity 2.0 beta 2 for Silverlight in my Windows
We're trying to use Dependency Injection for a WCF Service. The Service has a
Im trying to reference this config file that is in the same folder as
I have a fairly straight-forward scenario that I am trying to solve but I'm
I have been trying to use difference units (%, em, pt) to make a
Is it possible to use unityscript and boo together( in the same project) in
Is it possible to make unity try all defined constructors starting with the one
My company is looking into ASP.NET and Prism. We are wondering how much code
I have an interface which takes a generic type interface IIFace<T> Then i have
I am getting the exception PDOException: You cannot serialize or unserialize PDO instances when

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.