title: XML Fields with Entity Framework Code First
body: <p>I'm using Entity Framework with the Code First model (pet project, and I love editing simple classes and having my schema updated automatically).  I have a class like follows:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[Table("Polygons")]
  public class Polygon
  {
      public int PolygonId { get; set; }
      public String Texture { get; set; }
  
      public virtual ICollection&lt;Point&gt; Points { get; set; }
  }
  
  [Table("Points")]
  public class Point
  {
      public int PolygonId { get; set; }
      public double X { get; set; }
      public double Y { get; set; }
  }
  </code></pre>
  
  <p>It's useful to me to store Polygons in the database, and be able to query their texture.  On the other hand, if I'm saving a polygon with 5,000 points to the database it takes forever to run that many inserts, and honestly, I'm never going to be querying Points except to retrieve an individual Polygon.</p>
  
  <p>What I'd love to do is get rid of "PolygonId" in the "Point" class, get rid of the "Points" table, and have the Polygon table look something like</p>
  
  <pre><code>PolygonId int PK
  Texture varchar(255)
  Points XML
  </code></pre>
  
  <p>And then have the points just serialize to a string that is saved directly into the table, yet unserializes back into an array of points.  Is there a way to either have EF do this, or to write a custom serializer/deserializer for the field, so at least it seems automatic when used throughout the code-base? </p>
  
  <p>Thanks,</p>
  
  <p>Dan</p>