title: EntityFrameworkCore is there a way to create EnumToStringConverter without passing the enum type as generic?
body: <p>I am trying to use <code>EntityFrameworkCore</code> ORM to interact with my databases. By default, <code>EntityFrameworkCore</code> seems to store enum as int instead of string. </p>
  
  <p>However, I would like to store the value in the database as a string. I can see that <code>EntityFrameworkCore</code> ships with a converter called <a href="https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions" rel="nofollow noreferrer">EnumToStringConverter</a>.</p>
  
  <p>I am trying to use reflection to setup the model builder so I don't have to manually build each model.</p>
  
  <p>The issue that I am running into is that <code>EnumToStringConverter</code> accepts a generic type which must be an <code>enum</code>. But since I am trying to use reflection here I am unable to pass the enum type when creating the converter </p>
  
  <p>Here is what my code look like so far</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      base.OnModelCreating(modelBuilder);
  
      // Get all DbSet&lt;&gt; properties that are defined in the DbContext
      var modelTypes = typeof(DataContext).GetProperties()
                                          .Where(x =&gt; x.PropertyType.IsGenericType &amp;&amp; x.PropertyType.GetGenericTypeDefinition() == typeof(DbSet&lt;&gt;))
                                          .Select(x =&gt; x.PropertyType.GetGenericArguments().First())
                                          .ToList();
  
      foreach (Type modelType in modelTypes)
      {
          var properties = modelType.GetProperties();
  
          foreach (var property in properties)
          {
              if (IsPrimaryKey(property))
              {
                  // At this point we know that the property is a primary key
                  modelBuilder.Entity(modelType)
                              .Property(property.Name)
                              .UseSqlServerIdentityColumn()
                              .Metadata.BeforeSaveBehavior = PropertySaveBehavior.Ignore;
  
                  continue;
              }
  
              if (property.PropertyType.IsEnum)
              {
                  // At this point we know that the property is an enum.
                  // Add the EnumToStringConverter converter to the property so that
                  // the value is stored in the database as a string instead of number 
                  var converter = new EnumToStringConverter(); // if somehow I can change this code to something like var `new EnumToStringConverter(property.PropertyType);` the code would work
  
                  modelBuilder.Entity(modelType)
                              .Property(property.Name)
                              .HasConversion(converter);
  
                  continue;
              }
  
          }
      }
  }
  </code></pre>
  
  <p>The only issue with the above code is how <a href="https://github.com/aspnet/EntityFrameworkCore/blob/77e08fc0fea6bad478eb0adc0e53f7a932b8ac01/src/EFCore/Storage/ValueConversion/EnumToStringConverter.cs" rel="nofollow noreferrer">EnumToStringConverter</a> is constructed. If somehow I can provide a <code>Type</code> to the constructor of the <code>EnumToStringConverter</code> instead of passing it as a generic argument that would solve the problem.</p>