Showing posts with label EntityFrameworkCore ValueConverter EF EFCore6 C#. Show all posts
Showing posts with label EntityFrameworkCore ValueConverter EF EFCore6 C#. Show all posts

Saturday, 16 July 2022

Using ValueConverter in EF Core 6 to handle custom data types

Usually we stick to the familiar data types, but EF Core 6 (Entity Framework) can also both save a custom data type and also load it up again. Usually, you would save the data type in a familiar representation and then have some mapper method map into the desired type in a data transfer object - DTO. In this example we will anyways look at an alternative, a ValueConverter. This have been supported since EF Core 2.1. We first off define a ValueConverter by inhering from the ValueConverter class. This takes a 'from' and 'to' generic type argument. We convert from a type into another type and vice versa. We send in two expressions to do these mappings to the base constructor. I have pushed the code here if you want to test out ValueConverter in EF Core yourself.
git clone https://github.com/toreaurstadboss/EfCore6ValueConversionTemplate.git Let's see how we for example can save a Color (System.Drawing.Color) via its color name and load it up again.
 
 
 using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
 using System.Drawing;
 using System.Linq.Expressions;

 namespace PublisherData
 {
 
    public class ColorValueConverter : ValueConverter<Color, string>
    {
        private static Expression<Func<Color, string>> ColorString =
            c => new string(c.Name);
        static Expression<Func<string, Color>> ColorStruct =
            s => Color.FromName(s);

        public ColorValueConverter() : base(ColorString, ColorStruct) { 
        }
    }

 } 
 
Next up we 'bulk-configure' a value converter for the Color type to have conversion set to this ValueConverter. Inside the db context class we do :
 
 
  protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
  {
  	configurationBuilder.Properties<Color>().HaveConversion(typeof(ColorValueConverter)); 
  }
 
 
We also define a migration for database and add a column in a table to save the color, which is defined as a string (varchar(100)) column, so we can test out the conversion.
 
  using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PublisherData.Migrations
{
    public partial class favcolor : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "FavoriteColor",
                table: "Authors",
                type: "nvarchar(100)",
                maxLength: 100,
                nullable: true);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "FavoriteColor",
                table: "Authors");
        }
    }
}

 
A small console app then tests out the Value conversion :
 
 
 // See https://aka.ms/new-console-template for more information
 using Microsoft.EntityFrameworkCore;
 using PublisherData;
 using System.Drawing;

 Console.WriteLine("Posting a new author - testing out ValueConverter feature of EF Core 6");


 var options = new DbContextOptionsBuilder<PubContext>()
    .UseSqlServer("Data Source = somedevmachine\\sqlexpress01; Initial Catalog = PubDatabase_M12_Api_EfCoreCourse; Trusted_Connection=true");


 using var pubContext = new PubContext(options.Options);

 var authorInsert = new PublisherDomain.Author
  {
    FirstName = "Claude",
    LastName = "Monet",
    FavoriteColor = Color.Aquamarine
  }; 

 pubContext.Authors.Add(authorInsert);
 await pubContext.SaveChangesAsync();
 var insertedAuthor = pubContext.Authors.Find(authorInsert.AuthorId);
 string convertColorToArgbName(Color someColor) => Color.FromArgb(someColor.ToArgb()).Name;
 string insertedAuthorFavoriteColorArgb = convertColorToArgbName(authorInsert.FavoriteColor.Value);
 string aquaMarineColorArgb = convertColorToArgbName(Color.Aquamarine);

 Console.WriteLine($"Favorite color is expected? {insertedAuthorFavoriteColorArgb.Equals(aquaMarineColorArgb)}");

 
We save the favorite color and when we load up the entity again we load the expected color by checking the Argb value 'Name' property (the hexadecimal color value acutally). The output then reads:
Posting a new author - testing out ValueConverter feature of EF Core 6 Favorite color is expected? True This is a rather trivial example. We could have a more complex object which we wanted to save in a particular way to the database and load it up again. For some reason or another, that object might not just be serialized, saved and restored and loaded via deserialization. Anyways, ValueConverter is a feature which I wanted to test out in EF Core and it is an alternative to be at least aware of.