title: Custom type with automatic serialization/deserialization in EF Core
body: <p>I am using the <code>ical.net</code> library to work with recurrence rules and recurring events within my ASP.NET Core 2 app. I would like to be able to serialize a <code>CalendarEvent</code> object and save it in the database, and I'm looking for the best-practices approach to doing so.  I have considered using a non-mapped property to hold the actual object, and defining a string mapped property, and using the event handlers in the <code>DbContext</code> to serialize the object and set it to the string prop before saving the entity , and likewise to recreate the <code>CalendarEvent</code> object from the serialized string when building the entity. Something like the following:</p>
  
  <pre><code>public class AvailabilityRule: ApplicationEntity
  {
      ...
  
      [NotMapped]
      public CalendarEvent Event { get; set; }
  
  }
  
  public class ApplicationDbContext : AuditableDbContext
  {
      protected override void OnModelCreating(ModelBuilder builder)
      {
          builder.Entity&lt;AvailabilityRule&gt;().Property&lt;string&gt;("SerializedEvent");
      }
  
      public override int SaveChanges()
      {
          // Get instances of AvailabilityRule from ChangeTracker, and
          // set the serialized property
      }
  }
  </code></pre>
  
  <p>I'm assuming there is a similar method I can override to do the opposite upon pulling the record out of the database, but I don't know what it is.</p>
  
  <p>Being new to ASP.NET and Entity Framework, I'm concerned about doing things The Right Way<sup>TM</sup>, so I'm interested i knowing if there is a better way.  I can't find much information online about this topic.</p>