Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception message for instance or inline value converter with compiled model #25738

Closed
rlgordey opened this issue Aug 26, 2021 · 3 comments · Fixed by #25816
Closed
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@rlgordey
Copy link

I have a Customer Entity configured in my DbContext.ModelBuilder as:

modelBuilder.Entity<Customer>()
    .HasKey(k => k.Custnmbr);

modelBuilder.Entity<Customer>()
    .Property(e => e.Custnmbr)
    .HasConversion(v => v.TrimEnd(), v => v.TrimEnd());

The entity that was scaffolded is:

[Table("RM00101")]    
public class Customer : INotifyPropertyChanged
{
    [Key]
    [Column("CUSTNMBR", TypeName = "char(15)")]
    [StringLength(15)]
    public string Custnmbr { get; set; }
    ...
}

When I run Optimize-DbContext I get this exception:

System.InvalidOperationException: The property 'Customer.Custnmbr' has a value converter configured. Use 'HasConversion' to configure the value converter type.
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.Create(IProperty property, Dictionary`2 propertyVariables, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.CreateEntityType(IEntityType entityType, IndentedStringBuilder mainBuilder, IndentedStringBuilder methodBuilder, SortedSet`1 namespaces, String className, Boolean nullable)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.GenerateEntityType(IEntityType entityType, String namespace, String className, Boolean nullable)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.GenerateModel(IModel model, CompiledModelCodeGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CompiledModelScaffolder.ScaffoldModel(IModel model, String outputDir, CompiledModelCodeGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.Optimize(String outputDir, String modelNamespace, String contextTypeName)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OptimizeContextImpl(String outputDir, String modelNamespace, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OptimizeContext.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The property 'Customer.Custnmbr' has a value converter configured. Use 'HasConversion' to configure the value converter type.

Even though it is using 'HasConversion'.

EF Core version: 6.0.0-rc.2.21426.10
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: net6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.0.0 Preview 3.1

@ajcvickers
Copy link
Contributor

@rlgordey The key part about this message is the word "type" in, "Use 'HasConversion' to configure the value converter type." This means you need to create a ValueConverter type and configure that type in the model, rather than using inline conversion expressions. We will try to make this exception message clearer.

@rlgordey
Copy link
Author

@ajcvickers I have tried ...

var converter = new ValueConverter<string, string>(
                v => v.PadRight(15),
                v => v.TrimEnd());

modelBuilder.Entity<Customer>()
    .Property(e => e.Custnmbr)
    .HasConversion(converter);

With the same error.

@ajcvickers
Copy link
Contributor

@rlgordey Hopefully this is clearer:

The property 'Customer.Custnmbr' has a value converter configured using a ValueConverter instance or inline expressions.
Instead, create a type that inherits from ValueConverter and use 'HasConversion<ConverterType>()' or
'HasConversion(Type converterType)' to configure the value converter.

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 1, 2021
@ajcvickers ajcvickers changed the title Exception when trying to run Optimize-DbContext Improve exception message for instance or inline value converter with compiled model Sep 1, 2021
ajcvickers added a commit that referenced this issue Sep 2, 2021
…ompiled model (#25816)

* Be more explicit about which overloads of HasConversion to use with compiled model

Fixes #25738

* Update src/EFCore.Design/Properties/DesignStrings.resx

Co-authored-by: Andriy Svyryd <[email protected]>

* Update src/EFCore.Design/Properties/DesignStrings.resx

Co-authored-by: Andriy Svyryd <[email protected]>

Co-authored-by: Andriy Svyryd <[email protected]>
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-rc2 Sep 2, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-rc2, 6.0.0 Nov 8, 2021
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants