... because you have to make modifications to the defaults, then it must be called CreateWebHostBuilder for Entity Framework migrations to work.
https://stackoverflow.com/a/54222348/2496266
This information should be in this section:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation#from-application-services
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
you have to make modifications to the defaults
What modifications; which defaults?
The tools should work with several different ASP.NET Core patterns:
"The defaults": CreateDefaultBuilder().
Modifications: Such as those specified in the advanced configuration options.
I am not sure about the BuildWebHost and CreateHostBuilder options, but I am just pointing out that the method EF Core Migrations looks for is CreateWebHostBuilder. MVC Core doesn't require you to call it that, but EF Core Migrations does, if that makes sense.
Thanks!
I'm not sure I follow; you can use those advanced options and still return a web host builder. We don't require you to call CreateDefaultBuilder.
MVC Core doesn't require you to call it
CreateWebHostBuilder a well-known pattern used by MVC design-time tools for testing, scaffolding, etc. it is not exclusive to (or even defined by) EF Core.
https://github.com/chaim1221/Machete/commit/c09c740eb2140b28e27aa971c15d4786cc5acb86
"CustomWebHostBuilder":
Machete.Data:$ dotnet ef migrations --verbose add InitialCreate --startup-project ../Machete.Web
Using project '/Users/celiyah/Code/machete/scratch/Machete.Data/Machete.Data.csproj'.
Using startup project '../Machete.Web/Machete.Web.csproj'.
Writing '/Users/celiyah/Code/machete/scratch/Machete.Data/obj/Machete.Data.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/var/folders/9h/t5gjn23d4378p7ysh7rwnp2r0000gn/T/tmpLaqobx.tmp /verbosity:quiet /nologo /Users/celiyah/Code/machete/scratch/Machete.Data/Machete.Data.csproj
Writing '../Machete.Web/obj/Machete.Web.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/var/folders/9h/t5gjn23d4378p7ysh7rwnp2r0000gn/T/tmpajVNwz.tmp /verbosity:quiet /nologo ../Machete.Web/Machete.Web.csproj
dotnet build ../Machete.Web/Machete.Web.csproj /verbosity:quiet /nologo
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:08.90
dotnet exec --depsfile /Users/celiyah/Code/machete/scratch/Machete.Web/bin/Debug/netcoreapp2.2/Machete.Web.deps.json --additionalprobingpath /Users/celiyah/.nuget/packages --additionalprobingpath /usr/local/share/dotnet/sdk/NuGetFallbackFolder --runtimeconfig /Users/celiyah/Code/machete/scratch/Machete.Web/bin/Debug/netcoreapp2.2/Machete.Web.runtimeconfig.json /usr/local/share/dotnet/sdk/2.2.106/DotnetTools/dotnet-ef/2.2.4/tools/netcoreapp2.2/any/tools/netcoreapp2.0/any/ef.dll migrations add InitialCreate --assembly /Users/celiyah/Code/machete/scratch/Machete.Web/bin/Debug/netcoreapp2.2/Machete.Data.dll --startup-assembly /Users/celiyah/Code/machete/scratch/Machete.Web/bin/Debug/netcoreapp2.2/Machete.Web.dll --project-dir /Users/celiyah/Code/machete/scratch/Machete.Data/ --language C# --working-dir /Users/celiyah/Code/machete/scratch/Machete.Data --verbose --root-namespace Machete.Data
Using assembly 'Machete.Data'.
Using startup assembly 'Machete.Web'.
Using application base '/Users/celiyah/Code/machete/scratch/Machete.Web/bin/Debug/netcoreapp2.2'.
Using working directory '/Users/celiyah/Code/machete/scratch/Machete.Web'.
Using root namespace 'Machete.Data'.
Using project directory '/Users/celiyah/Code/machete/scratch/Machete.Data/'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding IWebHost accessor...
No CreateWebHostBuilder(string[]) method was found on type 'Machete.Web.Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'MacheteContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'MacheteContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 ---> System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean skipCheckThis, Boolean fillCache)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass12_3.<FindContextTypes>b__13()
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass12_3.<FindContextTypes>b__13()
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)
Unable to create an object of type 'MacheteContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
"CreateWebHostBuilder":
Machete.Data:$ dotnet ef migrations --verbose add InitialCreate --startup-project ../Machete.Web
Using project '/Users/celiyah/Code/machete/scratch/Machete.Data/Machete.Data.csproj'.
Using startup project '../Machete.Web/Machete.Web.csproj'.
Writing '/Users/celiyah/Code/machete/scratch/Machete.Data/obj/Machete.Data.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/var/folders/9h/t5gjn23d4378p7ysh7rwnp2r0000gn/T/tmp8kOAVG.tmp /verbosity:quiet /nologo /Users/celiyah/Code/machete/scratch/Machete.Data/Machete.Data.csproj
Writing '../Machete.Web/obj/Machete.Web.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/var/folders/9h/t5gjn23d4378p7ysh7rwnp2r0000gn/T/tmpV0okMu.tmp /verbosity:quiet /nologo ../Machete.Web/Machete.Web.csproj
dotnet build ../Machete.Web/Machete.Web.csproj /verbosity:quiet /nologo
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:08.72
dotnet exec --depsfile /Users/celiyah/Code/machete/scratch/Machete.Web/bin/Debug/netcoreapp2.2/Machete.Web.deps.json --additionalprobingpath /Users/celiyah/.nuget/packages --additionalprobingpath /usr/local/share/dotnet/sdk/NuGetFallbackFolder --runtimeconfig /Users/celiyah/Code/machete/scratch/Machete.Web/bin/Debug/netcoreapp2.2/Machete.Web.runtimeconfig.json /usr/local/share/dotnet/sdk/2.2.106/DotnetTools/dotnet-ef/2.2.4/tools/netcoreapp2.2/any/tools/netcoreapp2.0/any/ef.dll migrations add InitialCreate --assembly /Users/celiyah/Code/machete/scratch/Machete.Web/bin/Debug/netcoreapp2.2/Machete.Data.dll --startup-assembly /Users/celiyah/Code/machete/scratch/Machete.Web/bin/Debug/netcoreapp2.2/Machete.Web.dll --project-dir /Users/celiyah/Code/machete/scratch/Machete.Data/ --language C# --working-dir /Users/celiyah/Code/machete/scratch/Machete.Data --verbose --root-namespace Machete.Data
Using assembly 'Machete.Data'.
Using startup assembly 'Machete.Web'.
Using application base '/Users/celiyah/Code/machete/scratch/Machete.Web/bin/Debug/netcoreapp2.2'.
Using working directory '/Users/celiyah/Code/machete/scratch/Machete.Web'.
Using root namespace 'Machete.Data'.
Using project directory '/Users/celiyah/Code/machete/scratch/Machete.Data/'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding IWebHost accessor...
Using environment 'Development'.
Using application service provider from IWebHost accessor on 'Program'.
Found DbContext 'MacheteContext'.
Finding DbContext classes in the project...
Using context 'MacheteContext'.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.2.4-servicing-10062 initialized 'MacheteContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: using lazy-loading proxies MigrationsAssembly=Machete.Data
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding design-time services referenced by assembly 'Machete.Web'.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'Machete.Web'...
No design-time services were found.
Writing migration to '/Users/celiyah/Code/machete/scratch/Machete.Data/Migrations/20190514150904_InitialCreate.cs'.
Writing model snapshot to '/Users/celiyah/Code/machete/scratch/Machete.Data/Migrations/MacheteContextModelSnapshot.cs'.
Done. To undo this action, use 'ef migrations remove'
Note the line that says No CreateWebHostBuilder(string[]) method was found on type 'Machete.Web.Program'.
You _do_ require us to call it CreateWebHostBuilder. QED.
@chaim1221 If you're having issues with this pattern, then please report them at https://github.com/aspnet/Extensions since that's where this functionality is defined and managed. As @bricelam says above, EF uses this mechanism provided by ASP.NET, but it's not inherently part of EF, so if there are issues with the mechanism, then they should be reported to the ASP.NET team.
I don't have an issue with it at all, I just think it should be explicitly spelled out in the documentation. One line saying that what EF is looking for is a method defined as CreateWebHostBuilder would have saved me considerable time in looking for the issue.
If this _is_ supposed to be enforced by ASP.NET, then sure, it's an ASP.NET issue. But really it's not a code issue at all. It's a documentation issue. The code works fine.
Have the same issue. Got an error Unable to create an object of type 'MyDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 and here am I but there is no even a line on this page saying me that I MUST create a method CreateWebHostBuilder in my Program.cs How should I guess this? Please add this to documentation.
Duplicate of #1050 and #1087
Most helpful comment
Have the same issue. Got an error Unable to create an object of type 'MyDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 and here am I but there is no even a line on this page saying me that I MUST create a method CreateWebHostBuilder in my Program.cs How should I guess this? Please add this to documentation.