title: Entity Framework Core - Setting Value Converter generically
body: <p>I'm currently trialing Entity Framework Core 2.1 with a view to using it in the company I work for's business applications. I've got most of the way in implementing Value Converters in my test project but my existing knowledge base has let me down at the last hurdle!</p>
  <h1>What I'm trying to do</h1>
  <p>My understanding is that for enum values, the built in type converters can convert from the enum value to the string equivalent (EnumToStringConverter) or from the enum value to it's numerical representation (EnumToNumberConverter). However we use a custom string value to represent the enum in our database, so I have written a custom EnumToDbStringEquivalentConvertor to do this conversion and the database string value is specified as an attribute on each of the enum values in my model.</p>
  <p>The code is as follows:</p>
  <p><strong>Model</strong></p>
  <pre><code>public class User
  {
      [Key] public int ID { get; set; }
      public EmployeeType EmployeeType { get; set; }
  }
  
  public enum EmployeeType
  {
      [EnumDbStringValue(&quot;D&quot;)]
      Director,
      [EnumDbStringValue(&quot;W&quot;)]
      Weekly,
      [EnumDbStringValue(&quot;S&quot;)]
      Salaried
  }
  </code></pre>
  <p><strong>DataContext</strong></p>
  <pre><code>public class MyDataContext : DbContext
  {
      public DbSet&lt;User&gt; Users { get; set; }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          foreach (var entityType in modelBuilder.Model.GetEntityTypes())
          {
              foreach (var property in entityType.GetProperties())
              {
                  if (property.ClrType.IsEnum)
                  {
                      property.SetValueConverter(new EnumToDbStringEquivalentConvertor&lt;EmployeeType&gt;());
                  }
              }
          }
      }
  }
  </code></pre>
  <p><strong>Value Converter</strong></p>
  <pre><code>public class EnumToDbStringEquivalentConvertor&lt;T&gt; : ValueConverter&lt;T, string&gt;
  {
      public EnumToDbStringEquivalentConvertor(ConverterMappingHints mappingHints = null) : base(convertToProviderExpression, convertFromProviderExpression, mappingHints)
      { }
  
      private static Expression&lt;Func&lt;T, string&gt;&gt; convertToProviderExpression = x =&gt; ToDbString(x);
      private static Expression&lt;Func&lt;string, T&gt;&gt; convertFromProviderExpression = x =&gt; ToEnum&lt;T&gt;(x);
  
      public static string ToDbString&lt;TEnum&gt;(TEnum tEnum)
      {
          var enumType = tEnum.GetType();
          var enumTypeMemberInfo = enumType.GetMember(tEnum.ToString());
          EnumDbStringValueAttribute enumDbStringValueAttribute = (EnumDbStringValueAttribute)enumTypeMemberInfo[0]
              .GetCustomAttributes(typeof(EnumDbStringValueAttribute), false)
              .FirstOrDefault();
  
          return enumDbStringValueAttribute.StringValue;
      }
  
      public static TEnum ToEnum&lt;TEnum&gt;(string stringValue)
      {
          // Code not included for brevity
      }
  }
  </code></pre>
  <p>This code (I'm glad to say) seems to be working without any issues.</p>
  <h1>My problem</h1>
  <p>The documentation around value converters seems to suggest the way we assign them in the OnModelCreating method is to physically assign each individual type converter to each individual property in the model. I don't want to have to do this - I want my model to be the driver. I'll implement this later but, for now, in the current version of the code I'm looping through the entity types in my model, checking the 'IsEnum' property value and then assigning the value converter at that point.</p>
  <p>My problem is that the SetValueConverter extension method that I'm using requires me to pass it a new instance of EnumToDbStringEquivalentConvertor, which in my example is hard coded to be EnumToDbStringEquivalentConvertor which works. However I don't want that to be hardcoded - I want to pass the entity type's ClrType.</p>
  <p>I have used reflection to create generic types and generic methods before but I can't seem to find the right code to get this working.</p>
  <p>This:</p>
  <pre><code>public class MyDataContext : DbContext
  {
      public DbSet&lt;User&gt; Users { get; set; }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          foreach (var entityType in modelBuilder.Model.GetEntityTypes())
          {
              foreach (var property in entityType.GetProperties())
              {
                  if (property.ClrType.IsEnum)
                  {
                      var converterType = typeof(EnumToDbStringEquivalentConvertor&lt;&gt;);
                      var genericConverterType = converterType.MakeGenericType(property.ClrType);
  
                      MethodInfo setValueConverterMethodInfo = typeof(MutablePropertyExtensions).GetMethod(&quot;SetValueConverter&quot;);
                      setValueConverterMethodInfo.Invoke(property,
                              new object[] { property, Activator.CreateInstance(genericConverterType) });
                  }
               }
           }
      }
  }
  </code></pre>
  <p>gives me an error of &quot;<strong>System.MissingMethodException</strong>: 'No parameterless constructor defined for this object.'&quot; on the GetModel method in Microsoft.EntityFrameworkCore.Infrastructure</p>
  <p>So my question is can anyone advise me of how I can pass my value converter generically to EF Core's 'SetValueConveter' method?</p>
  <p>Thank you in advance for your assistance.</p>