-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Closed
Copy link
Description
File a bug
If you have any non-constructable implementation of Microsoft.EntityFrameworkCore.Design.IDesignTimeServices in your application and you run dotnet ef migrations add you end up with an exception.
Include your code
[assembly: DesignTimeServicesReference("Data.Entity.ActualDesignTimeServices, MyProject")]
namespace Data.Entity;
#pragma warning disable EF1001
public abstract class DesignTimeServicesBase : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
{
DoSomething();
}
protected abstract void DoSomething();
}
public class ActualDesignTimeServices : DesignTimeServicesBase
{
protected override void DoSomething()
{
}
}Include stack traces
System.MissingMethodException: Cannot dynamically create an instance of type 'Data.Entity.DesignTimeServicesBase'. Reason: Cannot create an abstract class.
at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.ConfigureDesignTimeServices(Type designTimeServicesType, IServiceCollection services)
at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.ConfigureUserServices(IServiceCollection services)
at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.CreateServiceCollection(DbContext context)
at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.Build(DbContext context)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.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)
Include verbose output
Please include --verbose output when filing bugs about the dotnet ef or Package Manager Console tools.
Use triple-tick fences for tool output. For example:
C:\Stuff\AllTogetherNow\FiveOh>dotnet ef migrations add Test
Using project 'D:\Dev\Git\MyProject\MyProject\MyProject.csproj'.
Using startup project 'D:\Dev\Git\MyProject\MyProject\MyProject.csproj'.
Writing 'D:\Dev\Git\MyProject\MyProject\obj\MyProject.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\KUSC\AppData\Local\Temp\tmpxmo5k5.tmp /verbosity:quiet /nologo D:\Dev\Git\MyProject\MyProject\MyProject.csproj
Writing 'D:\Dev\Git\MyProject\MyProject\obj\MyProject.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\KUSC\AppData\Local\Temp\tmphd3bny.tmp /verbosity:quiet /nologo D:\Dev\Git\MyProject\MyProject\MyProject.csproj
Build started...
dotnet build D:\Dev\Git\MyProject\MyProject\MyProject.csproj /verbosity:quiet /nologo /p:PublishAot=false
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:02.80
Workload updates are available. Run `dotnet workload list` for more information.
Build succeeded.
dotnet exec --depsfile D:\Dev\Git\MyProject\MyProject\bin\Debug\net8.0\MyProject.deps.json --additionalprobingpath C:\Users\KUSC\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig D:\Dev\Git\
MyProject\MyProject\bin\Debug\net8.0\MyProject.runtimeconfig.json C:\Users\KUSC\.dotnet\tools\.store\dotnet-ef\8.0.6\dotnet-ef\8.0.6\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll migrations add InitialSchema --assembly D:\Dev\Git\MyProject\MyProject\bin\D
ebug\net8.0\MyProject.dll --project D:\Dev\Git\MyProject\MyProject\MyProject.csproj --startup-assembly D:\Dev\Git\MyProject\MyProject\bin\Debug\net8.0\MyProject.dll --startup-project D:\Dev\Git\MyProject\MyProject\MyProject.csproj --project-dir D:\Dev\Git\MyProject\MyProject\ --root-namespace MyProject --language C# --framework net8.0 --nullable --working-dir D:\Dev\Git\MyProject --verbose
Using assembly 'MyProject'.
Using startup assembly 'MyProject'.
Using application base 'D:\Dev\Git\MyProject\MyProject\bin\Debug\net8.0'.
Using working directory 'D:\Dev\Git\MyProject\MyProject'.
Using root namespace 'MyProject'.
Using project directory 'D:\Dev\Git\MyProject\MyProject\'.
Remaining arguments: .
The Entity Framework tools version '8.0.6' is older than that of the runtime '8.0.8'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'MyProject'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'MyDbContext'.
Finding DbContext classes in the project...
Using context 'MyDbContext'.
Finding design-time services referenced by assembly 'MyProject'...
Finding design-time services referenced by assembly 'MyProject'...
Using design-time services from assembly 'MyProject'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'MyProject'...
Using design-time services from class 'DesignTimeServicesBase'.
System.MissingMethodException: Cannot dynamically create an instance of type 'MyProject.Data.Entity.DesignTimeServicesBase'. Reason: Cannot create an abstract class.
at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.ConfigureDesignTimeServices(Type designTimeServicesType, IServiceCollection services)
at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.ConfigureUserServices(IServiceCollection services)
at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.CreateServiceCollection(DbContext context)
at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.Build(DbContext context)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.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)
Cannot dynamically create an instance of type 'MyProject.Data.Entity.DesignTimeServicesBase'. Reason: Cannot create an abstract class.
Problem location
The problem is caused here:
| .Where(t => typeof(IDesignTimeServices).IsAssignableFrom(t)).Select(t => t.AsType()) |
EF doesn't check if the type can be really constructed and then fails subsequently.
Include provider and version information
EF Core version: 8.0.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11 Professional
IDE: CLI
Copilot