5

I am using the Repository pattern for EF and have ran into a problem in that I cannot figure out how to set the connection string for the DbContext through a variable. Currently my constructor is parameterless (it has to be to fit with he pattern) i.e.

IUnitOfWork uow = new UnitOfWork<EMDataContext>();
DeviceService deviceService = new DeviceService(uow);
var what = deviceService.GetAllDevices();


public UnitOfWork()
{
    _ctx = new TContext();
    _repositories = new Dictionary<Type, object>();
    _disposed = false;
}

EMDataContext used to take a string in its constructor to define the ConnectionString but can no longer do that so how do I actually tell the EMDataContext what to connect to when its created in this fashion?

4
  • what happenes in new UnitOfWork<EMDataContext>(); ? Commented Aug 23, 2013 at 11:45
  • 1
    Do you need to connect to more than one database? If not, use web.config or app.config files. Commented Aug 23, 2013 at 11:55
  • @JensKloster Updated. ta - Not at the moment, the question is how to pass in the connection string? Commented Aug 23, 2013 at 12:10
  • @Chris ok. What is known about TContext ? does it inherit something that allows you to set a connctionstring? Also - a .config file is a good place for the connectionstring. EF will look for it there if you don't directly specify where it is. Commented Aug 23, 2013 at 12:28

1 Answer 1

3

Your question can be rewritten as "How to pass an argument to a generic type constructor with a new() constraint".

From MSDN:

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.

Since the bare Entity Framework context doesn't contain a parameter-less constructor, I assume your EMDataContext is your custom context that derives from it:

public class EMDataContext : DbContext
{
      // parameterless ctor, since you're using new() in UnitOfWork<TContext>
      public EMDataContext() : base(???)
      {
      }

      public EMDataContext(string connectionString) : base(connectionString)
      {
      }
}

Now, I would argue that your EMDataContext is incapable of having a parameter-less constructor, hence incapable of the new() constraint as well, especially when you're saying that you do want to pass a connection-string parameter.

Try to change your UnitOfWork to accept an already-initialized context in it's constructor (common pattern):

public class UnitOfWork<TContext>
{
    public UnitOfWork(TContext ctx)
    {
        _ctx = ctx;
    }
}

Alternatively (if you still want to "fit the pattern"), try to instantiate the context using Activator:

public class UnitOfWork<TContext>
{
    public UnitOfWork(string connectionString)
    {
        _ctx = (TContext)Activator.CreateInstance(typeof(TContext), new[] { connectionString });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.