Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML columns in a Code-First application

Tags:

I'm trying to create an XML column in Code First. I'm well aware Entity Framework doesn't fully support XML columns, and that it reads them as a string. That's fine. I would still like the column type to be XML, though. Here's my class:

class Content {     public int ContentId { get; set; }      [Column(TypeName="xml")]     public string XmlString { get; set; }      [NotMapped]     public XElement Xml { get { ... } set { ... } }  } 

Problem is, that Code First Migrations completely ignores the Column attribute and creates the field as an nvarchar(max) . I tried using [DataType("xml")], but that, too, didn't work.

Is this a migration bug?

like image 959
zmbq Avatar asked Sep 27 '12 23:09

zmbq


1 Answers

Have you tried:

public String XmlContent { get; set; }  public XElement XmlValueWrapper {     get { return XElement.Parse(XmlContent); }     set { XmlContent = value.ToString(); } }  public partial class XmlEntityMap : EntityTypeConfiguration<XmlEntity> {     public XmlEntityMap()     {         // ...         this.Property(c => c.XmlContent).HasColumnType("xml");          this.Ignore(c => c.XmlValueWrapper);     } } 
like image 162
TDaver Avatar answered Oct 03 '22 17:10

TDaver