Error, Must specify valid information for parsing in the string, when using EF Core Value Conversion

asp.net-core entity-framework-core

Question

I'm attempting to utilize EF Core's new Value Conversion on an enumeration based on the documentation (https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions). In the SQL Database database, I want to save the enumeration as a text.

The entity and enumeration are shown here.

public enum InputSetType
{
    TypeA, TypeB
}

public class MonthlyInputSet
{
    public int Id { get; set; }
    public InputSetType Type { get; set; }
}

I set up the MonthlyInputSet Entity as follows:

public class MonthlyInputSetConfiguration : IEntityTypeConfiguration<MonthlyInputSet>
{
    public void Configure(EntityTypeBuilder<MonthlyInputSet> builder)
    {
        builder.Property(mis => mis.Type).HasConversion(v => v.ToString(), v => (InputSetType)Enum.Parse(typeof(InputSetType), v));
    }
}

I attempt to perform a simple query to get this data, but it fails. The issue is:

var saved = await _context.MonthlyInputSets.Include(mis => mis.InsertedBy)
                                                     .Include(mis => mis.UpdatedBy)
                                                     .Include(mis => mis.MonthlyInputs)
                                                         .ThenInclude(mi => mi.EmissionsUnit)
                                                     .FirstOrDefaultAsync(mis => mis.Id == id);

However, this query fails with the message "ArgumentException: Must supply sufficient information for parsing in the string" on the first line. Therefore, I assume that I have not set up the conversion of the string in the table to the enum in C# correctly.

A complete raw stack trace

enter image description here

I made sure the database was returning the right string value. It is not a blank string or null, respectively. The text value that was given exactly matches one of the enum members.

 public void Configure(EntityTypeBuilder<MonthlyInputSet> builder)
    {
        builder.Property(mis => mis.Type).HasConversion(
            convertToProviderExpression: v => v.ToString(), 
            convertFromProviderExpression: v => Troubleshooting(v)
        );
    }

    private InputSetType Troubleshooting(string v)
    {
        return (InputSetType)Enum.Parse(typeof(InputSetType), v);
    }

These two photos demonstrate how the second item of the enum that is assigned to this field's second member appears exactly once in each row of the database type column.

enter image description hereenter image description here

1
0
10/14/2019 3:56:31 PM

Popular Answer

ZZZ_tmp
0
10/14/2019 2:40:56 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow