Type Conversion

Conversioni dai tipi .NET ai tipi del database utilizzato

Value converters allow property values to be converted when reading from or writing to the database. This conversion can be from one value to another of the same type (for example, encrypting strings) or from a value of one type to a value of another type (for example, converting enum values to and from strings in the database).

Value conversions are defined on properties in the OnModelCreating of your DbContext. For example1, consider an enum and entity type defined as:

 1public class Rider
 2{
 3    public int Id { get; set; }
 4    public EquineBeast Mount { get; set; }
 5}
 6
 7public enum EquineBeast
 8{
 9    Donkey,
10    Mule,
11    Horse,
12    Unicorn
13}

Conversions can be configured in OnModelCreating to store the enum values as strings such as “Donkey”, “Mule”, etc. in the database; you simply need to provide one function which converts from the ModelClrType to the ProviderClrType, and another for the opposite conversion:

1protected override void OnModelCreating(ModelBuilder modelBuilder)
2{
3    modelBuilder
4        .Entity<Rider>()
5        .Property(e => e.Mount)
6        .HasConversion(
7            v => v.ToString(),
8            v => (EquineBeast)Enum.Parse(typeof(EquineBeast), v));
9}

The ValueConverter can instead be created explicitly. For example:

 1protected override void OnModelCreating(ModelBuilder modelBuilder)
 2{
 3    var converter = new ValueConverter<EquineBeast, string>(
 4        v => v.ToString(),
 5        v => (EquineBeast)Enum.Parse(typeof(EquineBeast), v));
 6
 7    modelBuilder
 8        .Entity<Rider>()
 9        .Property(e => e.Mount)
10        .HasConversion(converter);
11}

Built-in converters

EF Core ships with a set of pre-defined ValueConverter<TModel,TProvider> classes, found in the Microsoft.EntityFrameworkCore.Storage.ValueConversion namespace. In many cases EF will choose the appropriate built-in converter based on the type of the property in the model and the type requested in the database, as shown above for enums. For example, using .HasConversion<int>() on a bool property will cause EF Core to convert bool values to numerical zero and one values:

1protected override void OnModelCreating(ModelBuilder modelBuilder)
2{
3    modelBuilder
4        .Entity<User>()
5        .Property(e => e.IsActive)
6        .HasConversion<int>();
7}

A list of predefined type converters can be found on the EF Core docs pages.

Pre-defined conversions

For common conversions for which a built-in converter exists there is no need to specify the converter explicitly. Instead, just configure which provider type should be used and EF will automatically use the appropriate built-in converter. Enum to string conversions are used as an example above, but EF will actually do this automatically if the provider type is configured:

1modelBuilder
2    .Entity<Rider>()
3    .Property(e => e.Mount)
4    .HasConversion<string>();

The same thing can be achieved by explicitly specifying the column type. For example, if the entity type is defined like so:

1public class Rider
2{
3    public int Id { get; set; }
4
5    [Column(TypeName = "varchar(24)")]
6    public EquineBeast Mount { get; set; }
7}

Then the enum values will be saved as strings in the database without any further configuration in OnModelCreating.