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 ?
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!).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.IDbContextFactory
is no good for injection, it simply won't work- by design. Can't see the point of it yet...