Efcore: Add-Migration exception - The current CSharpHelper cannot scaffold literals of type 'System.Func`3

Created on 2 Dec 2016  路  8Comments  路  Source: dotnet/efcore

Exception is thrown when adding new migration to the project. Why?

Steps to reproduce

Open following project and try to add new migration.

This is new Web App project, I've just added some entities classes.

The issue

When trying to add a new migration including _MessageBase_ and _InvitationMessage_ entities, following exception is thrown:

Exception message: _The current CSharpHelper cannot scaffold literals of type 'System.Func`3[Microsoft.EntityFrameworkCore.Metadata.IProperty,Microsoft.EntityFrameworkCore.Metadata.IEntityType,Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator]'. Configure your services to use one that can._

Stack trace:
System.InvalidOperationException: The current CSharpHelper cannot scaffold literals of type 'System.Func3[Microsoft.EntityFrameworkCore.Metadata.IProperty,Microsoft.EntityFrameworkCore.Metadata.IEntityType,Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator]'. Configure your services to use one that can. at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpHelper.UnknownLiteral(Object value)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateAnnotation(IAnnotation annotation, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateAnnotations(IReadOnlyList1 annotations, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GeneratePropertyAnnotations(IProperty property, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperty(IProperty property, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperties(IEnumerable1 properties, 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.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.Execute(CommonOptions commonOptions, String name, String outputDir, String context, String environment, Action1 reporter)
at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.<>c__DisplayClass0_0.b__0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
The current CSharpHelper cannot scaffold literals of type 'System.Func3[Microsoft.EntityFrameworkCore.Metadata.IProperty,Microsoft.EntityFrameworkCore.Metadata.IEntityType,Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator]'. Configure your serv ices to use one that can.

Further technical details

Operating system: WIN10
Visual Studio version: VS 2015

Most helpful comment

Try upgrading the EF tools packages to 1.0.0-preview3-final and installing the 1.1.0 version of Microsoft.EntityFrameworkCore.Design. I don't think preview2 works with 1.1.

After install "Microsoft.EntityFrameworkCore.Design" work for me, thanks

All 8 comments

Try upgrading the EF tools packages to 1.0.0-preview3-final and installing the 1.1.0 version of Microsoft.EntityFrameworkCore.Design. I don't think preview2 works with 1.1.

After updating NuGet version and then all packages (including installing dotnetcore 1.1 instead of 1.0), migrations are working OK. Updated _project.json_ is commited to the project.

Thank you!

@bricelam

I'm facing the same issue.
In my model I'm trying to call:

protected override void OnModelCreating(ModelBuilder builder)
{
  base.OnModelCreating(builder);

  builder.Entity<User>().Property(a => a.Email).
    HasAnnotation("MinLength", new MinLengthAttribute(5));
}

When trying to apply migrations, I get the following error:

InvalidOperationException: The current CSharpHelper cannot scaffold literals of type 'System.ComponentModel.DataAnnotations.MinLengthAttribute'. Configure your services to use one that can.

System.InvalidOperationException: The current CSharpHelper cannot scaffold literals of type 'System.ComponentModel.DataAnnotations.MinLengthAttribute'. 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.GenerateAnnotations(IReadOnlyList`1 annotations, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GeneratePropertyAnnotations(IProperty property, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperty(String builderName, IProperty property, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperties(String builderName, IEnumerable`1 properties, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(String builderName, IReadOnlyList`1 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_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

My project file has the tools referenced as follows:

<ItemGroup>
 <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1"/>
 <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1"/>
</ItemGroup>

@weitzhandler - You are adding custom type annotation on model. Unless you override CSharpHelper service, it will not work.

What it your intention to add that annotation on the model?

@smitpatel
What it your intention to add that annotation on the model?

I thought I'd keep the model clean of attributes but I guess never mind.
Thanks for your quick response.

The point is, even if you put that annotation in EF model, EF does not understand it and will not do anything around it apart from throwing exception when it needs to print it out. If you are trying to use annotations for validation then you should put annotations on classes rather than in EF model. EF does not do any validation. Other validation frameworks based on annotations are required to do due processing.

Try upgrading the EF tools packages to 1.0.0-preview3-final and installing the 1.1.0 version of Microsoft.EntityFrameworkCore.Design. I don't think preview2 works with 1.1.

After install "Microsoft.EntityFrameworkCore.Design" work for me, thanks

@aliimalii your comment was racist and inappropriate. We marked it as such. Please follow our Code of Conduct in future. If there is similar unacceptable behavior in future on our repos, we may decide to ban you from our organization.
We have reported your comment above to GitHub for further processing and/or actions.

Was this page helpful?
0 / 5 - 0 ratings