1

I have a project that works for a single database. Now I need to get it to work with a second (within the same project) that has the same data structure. So I am using the same model and am trying to pass in the Data Connection name upon calling my Data Context Class. Unfortunately I am receiving the following error:

The type String cannot be constructed. You must configure the container to supply this value.

Here is the code that I tried:

public UniversityContext(string context)
            : base(context){

   }

When I looked for answers I found this answer for that error and it recommends, having a parameter-less constructor as well. I tried that and still the same issue.

Here it is with the parameter-less constructor:

public UniversityContext()
            : base("UniversityConnection")
        {

        }

        public UniversityContext(string context)
            : base(context)
        {

        }

In case it matters I am using Unity.

Community
  • 1
  • 1
djblois
  • 963
  • 1
  • 17
  • 52

1 Answers1

1

Unity automatically tries to use the most greedy constructor. In your case this is:

public UniversityContext(string context)

You can configure this with an injection constructor, without seeing your code something like this:

container.RegisterType<UniversityContext>(
          new InjectionConstructor("UniversityContext"));
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
  • Hutchonoid, thank you for your response but where would I put the injection constructor? And how do I get it ti change depending on the different connection. Btw, I updated the original post a little (updated the connection name). – djblois May 20 '16 at 14:46
  • @djblois No problem, I usually have a bootstrapper that configures all the registered types. You could read the value in from web.config or determine it at runtime depending on your requirements. This is a good article: https://msdn.microsoft.com/en-us/library/dn178463%28v=pandp.30%29.aspx?f=255&MSPPError=-2147217396 – hutchonoid May 20 '16 at 14:54