Color Properties in Entity Framework

  • 2 minutes to read

The following example demonstrates how to store a System.Drawing.Color property value in the database managed by the Entity Framework ORM. The approach is based on converting the System.Drawing.Color type to the System.Int32 using an auxiliary hidden integer property.

using System.ComponentModel.DataAnnotations.Schema;
using System.Drawing;
[Browsable(false), Column("Color")]
public int Argb {
    get { return fColor.ToArgb(); }
    set { fColor = Color.FromArgb(value); }
private Color fColor;
public Color Color {
    get { return fColor; }
    set { fColor = value; }

If the type does not have an appropriate conversion method (Color.ToArgb in this example), implement a Type Converter and use it any time you need to covert a value from the type you need to store to an ORM-friendly type.


Object Space is not marked modified when the non-persistent Color property is changed. If this behavior does not fit your requirements, set the BaseObjectSpace.NonPersistentChangesEnabled property to true.