c# - XML Fields with Entity Framework Code First -


i'm using entity framework code first model (pet project, , love editing simple classes , having schema updated automatically). have class follows:

[table("polygons")] public class polygon {     public int polygonid { get; set; }     public string texture { get; set; }      public virtual icollection<point> points { get; set; } }  [table("points")] public class point {     public int polygonid { get; set; }     public double x { get; set; }     public double y { get; set; } } 

it's useful me store polygons in database, , able query texture. on other hand, if i'm saving polygon 5,000 points database takes forever run many inserts, , honestly, i'm never going querying points except retrieve individual polygon.

what i'd love rid of "polygonid" in "point" class, rid of "points" table, , have polygon table like

polygonid int pk texture varchar(255) points xml 

and have points serialize string saved directly table, yet unserializes array of points. there way either have ef this, or write custom serializer/deserializer field, @ least seems automatic when used throughout code-base?

thanks,

dan

i think have add property , write code serialization.

this should it:

[table("polygons")] public class polygon {     public int polygonid { get; set; }     public string texture { get; set; }      [notmapped]     public virtual icollection<point> points { get; set; }      [column("points")]     [editorbrowsable(editorbrowsablestate.never)]     [debuggerbrowsable(debuggerbrowsablestate.never)]     public string pointsxml     {                 {             var serializer = new xmlserializer(typeof (list<point>));             using (var stringwriter = new stringwriter())             {                 serializer.serialize(stringwriter, points.tolist());                 stringwriter.flush();                 return stringwriter.tostring();             }         }         set         {             var serializer = new xmlserializer(typeof(list<point>));             using (var stringreader = new stringreader(value))             {                 points = (list<point>) serializer.deserialize(stringreader);             }         }     } } 

the editorbrowsable , debuggerbrowsable attributes optional , keep xml property showing in intellisense (when type used library) , debugger.


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -