Model First: how to add a property of type Color?

I am creating a new database using a model-based approach and I want to add a column to the type table System.Drawing.Color, but I do not have this option in the property list.

Is there a way to use more data types than those that are available?

+4
source share
2 answers

Another option here is to use the adapt response from Entity Framework 6 Code First - Custom Type Mapping

public class MyLittlePony {
    public string Name { get; set; }
    public Int32 Argb {
        get {
            return Color.ToArgb();
        }
        set {
            Color = Color.FromArgb(value);
        }
    }

    [NotMapped]
    public Color Color { get; set; }
}

The attribute NotMappedcauses the entity infrastructure not to try to map the Color property to a column in the database.

+10
source

, int ARGB:

public class ColorData
{
   public int Id { get; set; }

   public int Argb { get; set; }

   public System.Drawing.Color Color
   {
      get { return new System.Drawing.Color.FromArgb(this.Argb); }
      set 
      { 
         if(value!=null)
             this.Argb = value.ToArgb(); 
         else
             this.Argb = 0;
      }
   }
}

Color , :

protected void override OnModelCreating(ModelBuilder builder)
{
   var colorTable = builder.Entity<ColorData>();
   colorTable.HasKey(x => x.Id).HasColumnName("COLOR_ID");
   colorTable.Property(x => x.Argb).HasColumnName("COLOR_ARGB");
   colorTable.Ignore(x=>x.Color);

   colorTable.ToTable("COLOR_DATA");
}

Color:

ColorData data = new ColorData();
data.Color = Color.Black;
data.Color = Color.FromName("Black");
data.Color = Color.FromArgb( 0xFF000000 );
data.Color = Color.FromArgb( 255, 0, 0, 0 );

, , - XML.

, , SQL Server .

XML , . System.Drawing.Color :

<Color>
    <A>123</A>
    <R>45</R>
    <G>67</G>
    <B>89</B>
    <ScA>0.482352942</ScA>
    <ScR>0.026241‌​2224</ScR>
    <ScG>0.0561284944</ScG>
    <ScB>0.09989873</ScB>
</Color>

?

int :

SELECT COUNT(1) FROM Customer cust
INNER JOIN Color col
ON cust.FavouriteColorId = color.Id
WHERE col.Argb = 0xFF0000FF

XML:

SELECT COUNT(1) FROM Customer cust
INNER JOIN Color col
ON cust.FavouriteColorId = color.Id
WHERE col.ArgbXml.value('/Color/A') = N'255'
AND col.ArgbXml.value('/Color/R') = N'0'
AND col.ArgbXml.value('/Color/G') = N'0'
AND col.ArgbXml.value('/Color/B') = N'255'
+2

Source: https://habr.com/ru/post/1548467/


All Articles