title: UNITY: passing in a new datacontext each time?
body: <p>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</p>
  
  <p>Currently its failing on creating the repository, i think it can't resolve MyDataContext</p>
  
  <p>Before creating a constructor on "the repository" (see below) to take in the DataContext on my repository everything worked but now its failing..</p>
  
  <p>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</p>
  
  <pre><code>        container = new UnityContainer();
  
          Container.RegisterType&lt;MyDataContext, MyDataContext&gt;()
              .RegisterType&lt;IOfficeRepository, OfficeRepository&gt;()
              .RegisterType&lt;IOfficeService, OfficeService&gt;();
  </code></pre>
  
  <p>basically i have a service that calls the repository like so</p>
  
  <pre><code>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");
      }
  </code></pre>
  
  <p>here is my repository</p>
  
  <pre><code>public class OfficeRepository : IOfficeRepository
  {
      private MyDataContext db;
  
      public OfficeRepository (MyDataContext dataContext)
      {
          this.db = dataContext;
      }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I almost forgot i am doing this to create the service</p>
  
  <pre><code>officeService = Bootstrapper.Container.Resolve&lt;IOfficeService&gt;();
  </code></pre>
  
  <p><strong>EDIT - THE ERROR BEING GENERATED</strong></p>
  
  <pre><code> 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)
  </code></pre>
  
  <p><strong>EDIT - REMOVING Constructor on repository works</strong></p>
  
  <p>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</p>
  
  <pre><code>public class OfficeRepository : IOfficeRepository
  {
      private MyDataContext db new MyDataContext(); // CHANGE
  
      //public OfficeRepository (MyDataContext dataContext)
      //{
          //this.db = dataContext;
      //}
  </code></pre>
  
  <p><strong>EDIT - ACTUAL ERROR</strong> </p>
  
  <p>After digging deeper i have found this error ....</p>
  
  <pre><code>The type MyDataContext has multiple constructors of length 2. 
  Unable to disambiguate. (Strategy type DynamicMethodConstructorStrategy, index 0)
  (Strategy type BuildPlanStrategy, index 3)
  </code></pre>
  
  <p><strong>EDIT - TEST TO RESOLVE THE DATACONTEXT with 1 line of code</strong></p>
  
  <p>This also fails with the same error as above - multiple constructors</p>
  
  <pre><code>  MyDataContext test = Bootstrapper.Container.Resolve&lt;MyDataContext &gt;();
  </code></pre>
  
  <p><strong>EDIT - ALL CONSTRUCTORS ON MY DATACONTEXT</strong></p>
  
  <p>These were created by an exernal util but all should be well..</p>
  
  <pre><code>    [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();
      }
  </code></pre>
  
  <p><strong>EDIT - To demonstrate creating the DataContext in code without Unity works 100% without issue</strong> </p>
  
  <pre><code>   MyDataContext tes2t = new MyDataContext ();
  </code></pre>