In a project I need to set up an ASP.NET MVC (with .NET 4.6.1) but using the "new" EF Core for accessing the database.
Unfortunately every documentation explains only how to setup an ASP.NET Core MVC project.
I just gave it a try and when it comes to creating the database via the Package Manager Console I get the error message:
No parameterless constructor was found on 'DataContext'. Either add a parameterless constructor to 'DataContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'DataContext'
Yes, I don't have a parameterless constructor but neither does the example code of microsoft
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
I guess the problem is, that I don't register the DataContext in the Startup.cs which I don't have in an "old" ASP.NET MVC application.
Can anyone help me with this one?
A simple example
In Example.EF: Install EF Core, Microsft Dependency Injection. Create a class to support DI
In the Example.MVC, with the Application_Start in Global.asax or Startup with Owin
To run the migration:
We should have IDesignTimeDbContextFactory to support to run the migration.
As per https://docs.microsoft.com/en-gb/ef/core/miscellaneous/cli/dbcontext-creation, you need to create a factory.
You can't inject a connection string here, but it's not an issue since this is only used for design-time functionality like creating migrations.