efcore: The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.

I’s using dotnet core 2 and EF core 2. I use CLI to generate migration files. In module which i try to generate Migration file, i use this factory:

`
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ChakavakIdentityDbContext> {

    public ChakavakIdentityDbContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder();
        //Database Migrations must use a relational provider. Microsoft.EntityFrameworkCore.InMemory is not a Relational provider and therefore cannot be use with Migrations.
        //builder.UseInMemoryDatabase("EmployeeDB");
        //You can point to use SQLExpress on your local machine
        var connectionstring = "Server=.;Database=Chakavak1; Trusted_Connection=True;MultipleActiveResultSets=true";
        builder.UseSqlServer(connectionstring);
        
        return new ChakavakIdentityDbContext(builder.Options);
    }
}

` And here is project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\Core\Chakava.Identity.Core.csproj">
      <Project>{745DB177-A077-496A-9FDE-79A320B8BD6B}</Project>
      <Name>Chakava.Identity.Core</Name>
    </ProjectReference>
    <ProjectReference Include="..\Identity\Chakavak.Identity.csproj" />
    <ProjectReference Include="..\Store\Chakavak.Identity.Store.csproj">
      <Project>{66A9E441-9F5C-4B39-8CDB-028E549C1675}</Project>
      <Name>Chakavak.Identity.Store</Name>
    </ProjectReference>
    <ProjectReference Include="..\Store\Chakavak.Identity.Store.csproj" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" 
                                     PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0-preview1-
                        final" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.1.0-preview1-
                                final" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.1.0-preview1-
                       final" />
  </ItemGroup>
</Project>

But when i run dotnet ef migrations add initial --verbose, i run into this message

` dotnet ef migrations add initial --verbose Using project ‘D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\Chakavak.Identity.EntityFrameworkCore.csproj’. Using startup project ‘D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\Chakavak.Identity.EntityFrameworkCore.csproj’. Writing ‘D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\obj\Chakavak.Identity.EntityFrameworkCore.csproj.EntityFrameworkCore.targets’… dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\masoud\AppData\Local\Temp\tmpB699.tmp /verbosity:quiet /nologo D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\Chakavak.Identity.EntityFrameworkCore.csproj Writing ‘D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\obj\Chakavak.Identity.EntityFrameworkCore.csproj.EntityFrameworkCore.targets’… dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\masoud\AppData\Local\Temp\tmpB959.tmp /verbosity:quiet /nologo D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\Chakavak.Identity.EntityFrameworkCore.csproj dotnet build D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\Chakavak.Identity.EntityFrameworkCore.csproj /verbosity:quiet /nologo

Build succeeded. 0 Warning(s) 0 Error(s)

Time Elapsed 00:00:08.74 dotnet exec --depsfile D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\bin\Debug\netcoreapp2.0\Chakavak.Identity.EntityFrameworkCore.deps.json --additionalprobingpath C:\Users\masoud.nuget\packages --additionalprobingpath “C:\Program Files\dotnet\sdk\NuGetFallbackFolder” --fx-version 2.0.0 “C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools.dotnet\2.0.1\tools\netcoreapp2.0\ef.dll” migrations add initial --assembly D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\bin\Debug\netcoreapp2.0\Chakavak.Identity.EntityFrameworkCore.dll --startup-assembly D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\bin\Debug\netcoreapp2.0\Chakavak.Identity.EntityFrameworkCore.dll --project-dir D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\ –verbose --root-namespace Chakavak.Identity.EntityFrameworkCore Using assembly ‘Chakavak.Identity.EntityFrameworkCore’. Using startup assembly ‘Chakavak.Identity.EntityFrameworkCore’. Using application base ‘D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\bin\Debug\netcoreapp2.0’. Using working directory ‘D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore’. Using root namespace ‘Chakavak.Identity.EntityFrameworkCore’. Using project directory 'D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore'. Finding DbContext classes… Finding IDesignTimeDbContextFactory implementations… Found IDesignTimeDbContextFactory implementation ‘ApplicationDbContextFactory’. Found DbContext ‘ChakavakIdentityDbContext’. Finding application service provider… Finding BuildWebHost method… No entry point was found for assembly ‘Chakavak.Identity.EntityFrameworkCore’. No application service provider was found. Finding DbContext classes in the project… Using DbContext factory ‘ApplicationDbContextFactory’. Using context ‘ChakavakIdentityDbContext’. Finding design-time services for provider ‘Microsoft.EntityFrameworkCore.SqlServer’… Using design-time services from provider ‘Microsoft.EntityFrameworkCore.SqlServer’. Finding IDesignTimeServices implementations in assembly ‘Chakavak.Identity.EntityFrameworkCore’… No design-time services were found. System.InvalidOperationException: The current CSharpHelper cannot scaffold literals of type ‘Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding’. Configure your services to use one that can. at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.UnknownLiteral(Object value) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateAnnotation(IAnnotation annotation, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypeAnnotations(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(String builderName, IReadOnlyList1 entityTypes, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(String builderName, IModel model, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMetadata(String migrationNamespace, Type contextType, String migrationName, String migrationId, IModel targetModel) at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace) 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_01.<Execute>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) The current CSharpHelper cannot scaffold literals of type ‘Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding’. Configure your services to use one that can. `

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 16 (3 by maintainers)

Commits related to this issue

Most upvoted comments

It worked for me: https://www.youtube.com/watch?v=i_fDTRoMuG0 I add in .csproj <ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup>

In terminal: dotnet add package Microsoft.EntityFrameworkCore.Design dotnet add package Microsoft.EntityFrameworkCore.qlServer

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />

FYI. Adding Microsoft.EntityFrameworkCore.Design to the DB project fixed it for me.

@mexahuk61 Because <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" /> includes 2.0 packages.

I came from dotnetcore 2.0.1 and ef 2.0.1 and upgraded to 2.1 for both. Then I got the error when trying to add a migration. After applying @krlosruiz suggestion and installing Microsoft.EntityFrameworkCore.Design in the db project, the issue was fixed.

@masoud-bahrami @mexahuk61 Looks like you are using a mixture of EF Core 2.0 packages and EF Core 2.1 preview1 packages. Was your intention to use 2.0 or 2.1 preview1?

I update all json packages to use EF 2.0.0

@Liero - You can use EF 2.1 preview package with netcoreapp2.0. Instead of referencing Microsoft.AspNetCore.All package add package for EF core provider package you would like to use.

@ajcvickers oh! You are right! AspNetCore.All package already contains EF. Moreover EF 2.1 depends on DependencyInjection >= 2.1.0 and other important packages… it’s strange from my point of view 😃 Thank you