IDesignTimeDbContextFactory Constructor with parameter
See original GitHub issueI implement IDesignTimeDbContextFactory for dotnet ef migrations add. My current code is:
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
public AppDbContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<AppDbContext>();
builder.UseSqlServer("Server=10.0.0.73\\MSSQLSERVER2016;Database=OpPISWebDevelopment;User Id=oppisweb;Password=oppisweb;Max Pool Size=10;Connection Timeout=100000;");
return new AppDbContext(builder.Options);
}
}
But I don’t want hard coded ConnectionString. I tried like this:
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
private IConfiguration config;
public ApplicationDbContextFactory(IConfiguration config)
{
this.config = config;
}
public AppDbContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<AppDbContext>();
builder.UseSqlServer(this.config["ConnectionString"]);
return new AppDbContext(builder.Options);
}
}
but I get error:
System.MissingMethodException: No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass12_0.<FindContextTypes>b__3() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
1 factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0
1.<Execute>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) No parameterless constructor defined for this object.
when executed
dotnet ef migrations add
How to get connection string from appsettings.json
?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:9
- Comments:12 (4 by maintainers)
I was finally able to figure this out. There were a few things that tripped me up:
To Start:
C:\Users\ben.rubinger\Desktop\EF Migrations Help\EF Migrations Help>dotnet ef migrations add new info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
C:\Users\ben.rubinger\Desktop\EF Migrations Help\EF Migrations Help>
From here, I moved into the data project directory: C:\Users\ben.rubinger\Desktop\EF Migrations Help\Data>dotnet ef migrations add new
No executable found matching command "dotnet-ef"
This was because I didn’t have:
in the relevant CSProj.
After I did this:
C:\Users\ben.rubinger\Desktop\EF Migrations Help\Data>dotnet ef migrations add new
C:\Users\ben.rubinger\Desktop\EF Migrations Help\Data>
Note that my solution did not involve following this advice. Instead, I finally figured out that I had to set the startup project: C:\Users\ben.rubinger\Desktop\EF Migrations Help\Data>dotnet ef migrations add new --startup-project “…\EF Migrations Help”
and it finally worked! It seemed counterintuitive that I need to run the command from the data directory, and point it to the original webapp directory, yet this gave me success. I’m posting this here in case others hit this. I struggled a bit with the bits and pieces of documentation.
Update: while the above is true for the sample project, for my actual project I had a last hurdle. My data project was targeting .netcoreapp2.0, and I got an error stating “The specified framework ‘Microsoft.NETCore.App’, version ‘2.0’ was not found”. I had to switch to .netstandard2.0 to finally get it working. Quite a few hurdles. I didn’t think this was a very strange scenario I was trying to achieve.
@MaklaCof The factory is not, by design, resolved from the service provider. The way to get your configuration into the factory is to duplicate or refactor and re-use the code you have in Main that sets up your configuration. In fact, if you factor this out appropriately into a BuildWebHost method that is just about configuration and not about running the application, then you should not need to have a factory at all. For more information, see: https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/