16

I am getting this error when I try to use code first migrations.

My context has a constructor with the connection name.

public class VeraContext : DbContext, IDbContext
{
    public VeraContext(string NameOrConnectionStringName = "VeraDB")
        : base(NameOrConnectionStringName)
    {
    }

    public IDbSet<User> Users { get; set; }
    public IDbSet<Product> Products { get; set; }
    public IDbSet<IntCat> IntCats { get; set; }
}

This connection name is injected with ninject when the project runs, I have also specified it as a default as in the above code but this did not help.

kernel.Bind<IDbContext>()
    .To<VeraContext>()
    .WithConstructorArgument("NameOrConnectionStringName", "VeraDB");

When I try to add migrations with "Enable-Migrations" is throws up the error:

The target context 'VeraData.EF.Infrastructure.VeraContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.

If I remove the constructor from VeraContext it will work but creates another database with VeraData.EF.Infrastructure.VeraContext as its name.

I presume that ninject only passes the connection string when the project runs and not when I use code first migrations. Anyway I can inject/provide a default for the connection name when using code first migrations ?

3
  • 1
    All the ninject stuff happens when someone somewhere is doing a Kernel.Get. You need to either add a default ctor or wait for someone to tell you how to hook the migrations stuff to construct objects via Ninject (which I guess is what you're doing!). Apr 12, 2013 at 8:50
  • You'd have to implement the IDbContextFactory for the results to be consistent (or your migration from code won't work etc.). Essentially you need a default ctor (that's the error) - but just implementing it would lead to problems. Apr 12, 2013 at 13:34
  • The IDbContextFactory is no good for injection, it simply won't work- by design. Can't see the point of it yet... Feb 17, 2014 at 15:49

4 Answers 4

20

Essentially you need a default ctor (that's the error) - but just implementing it would lead to problems.

You'd have to implement the IDbContextFactory for the results to be consistent (or your migration from code won't work etc.).

Migrations actually call your default constructor to make a connection. So you're other ctor won't matter much.

Here is the basic factory...

public class MyContextFactory : IDbContextFactory<MyContext>
{
    public MyContext Create()
    {
        return new MyDBContext("YourConnectionName");
    }
}

You should combine that with injection, to inject and construct your DbContext as you wish.

8
  • Could you explain what you mean further, you can't inject, as the Create() doesn't take parameters and there must be a default constructor, which is always the one called- hence, nowhere to inject. At least, no constructor-injection. Feb 17, 2014 at 15:50
  • @nicodemus13 it was a longtime ago, but as I remember - it's to ensure consistent DbContext construction. oftentimes EF invokes default ctor by automatism, whether you want it or not, so you need to have one, and custom ctor doesn't get called. Factory is primarily for EF calls, you can use the injection on DbContext normally. But b) as far as I recall most IoC containers (I don't use ninject much) have way of defining e.g. container.Create<MyDbContext> and then some anonymous method to engage the factory (if the factory is more complex so you need to go through it, otherwise just as I said) Feb 18, 2014 at 15:14
  • 1
    @NSGaga - Any reason why we cannot inject the DbContext or the connection string into the MyContextFactory through constructor and then return the DbContext instead of new'ing up a new DbContext in the create method?
    – Shiva Naru
    Apr 9, 2015 at 19:14
  • @dotnetter you could do all sorts of things - but, DbContext is usually short-lived, one off object - and (in 99% cases) shouldn't be saved, reused or even more used as a singleton (it could lead to issues). EF on the other side keeps the core info inside a static and it's reused over different instances, so that the whole create, tear-down is efficient. In short, there's no need to do such optimizations, if I'm getting right what you're after here. Apr 9, 2015 at 23:16
  • @NSGaga thanks. That makes sense. But I tried injecting (IoC) connection string name rather than hardcoding it and it didnt work.
    – Shiva Naru
    Apr 10, 2015 at 23:39
8

If you don't want to spend time looking into the IDbContextFactory option, and to get things working create a default constructor and hard-code the name of the connection string when calling the base DbContext:

public class CustomContext : DbContext
{
    public CustomContext() :base("name=Entities") {} 
}

SRC: http://www.appetere.com/Blogs/SteveM/April-2012/Entity-Framework-Code-First-Migrations

1
  • 2
    Isn't the whole point of using Ninject (or any IoC) so that the connection string name can be configured independently of the actual DbContext? So why would one hardcode the connection string name like this? Enable-Migrations provides a command line option to provide a connection string name to the command. The question I have is how can we then inject that connection string name into our DbContext classes when constructing them. Seems like that is what the OP was after too.
    – crush
    Sep 27, 2018 at 19:38
2

To complement @nccsbim071 answer, I have to add one more thing... this option doesn't like constructor with default parameters... for instance:

public MyContext(bool paramABC = false) : base("name=Entities") {...}

instead you have to create a non-parameter (default) constructor and the parameter-constructor like old fashion way.

public MyContext() :base("name=Entities") {...} 
public MyContext(bool paramABC) : this() {...}

NOTE:

  • Entities in this case means the connection string name... By convention, the name of the context is the same as the connection string name and since MyContext is not the same as Entities, it's necessary specify it manually.
0

In my situation I wanted to use the default connection factory, instead of explicitly providing one. Somewhere inside EF6 it'll try to lookup the factory, but it fails with this exception message. Stepping through the EF6 code, I found that Glimpse.Ado was wrapping the connection factory, which made the lookup fail to find a match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.