c# - Code-First 应用程序中的 XML 列

标签 c# xml ef-code-first entity-framework-migrations

我正在尝试在 Code First 中创建一个 XML 列。我很清楚 Entity Framework 并不完全支持 XML 列,而是将它们作为字符串读取。没关系。不过,我仍然希望列类型为 XML。这是我的类(class):

class Content
{
    public int ContentId { get; set; }

    [Column(TypeName="xml")]
    public string XmlString { get; set; }

    [NotMapped]
    public XElement Xml { get { ... } set { ... } }
 }

问题是,Code First 迁移完全忽略了 Column 属性并将该字段创建为 nvarchar(max) 。我尝试使用 [DataType("xml")],但这也没有用。

这是迁移错误吗?

最佳答案

你试过吗:

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);
    }
}

关于c# - Code-First 应用程序中的 XML 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12631290/

相关文章:

c# - 函数的返回值存储在哪里

c# - 如何使用 EF 代码优先 POCO 创建 View

xml - 是否有更快的方法来检查 LINQ to XML 中的 XML 元素并解析 bool?

php - 为什么这些 XML 标记会在我的 PHP 中产生错误?

entity-framework - 如何找到流利的API映射了哪个表?

ef-code-first - 如何以编程方式读取EF DbContext元数据?

c# - 在 ASP.NET MVC 4 C# Code First 中指定 ON DELETE NO ACTION

c# - 如何使用 SqlFunctions.DateName 获取小时部分

c# - 如何防止 GridView 中的重复数据?

java - 没有命名空间的 XML。针对多个 XSD 之一进行验证