【问题标题】:Update a column in table containing XML data using LINQ-to-SQL使用 LINQ-to-SQL 更新包含 XML 数据的表中的列
【发布时间】:2019-07-22 06:56:18
【问题描述】:

我正在使用 LINQ-to-SQL 在 SQL 中创建和维护一个表,其中包含发送到另一个应用程序的 XML 消息及其相应的响应。表中的每一行都包含请求消息或响应消息的详细信息以及相关消息的 ID(在响应消息的情况下为请求消息的 ID,反之亦然)。

这一切都很好,直到我尝试更新包含请求消息行中关联响应消息的 id 的列。为此,我使用以下形式的代码:

using (var MessageStore = new MessageStore(CONNECTION_STRING))
{
     // Get the Request message with the specified ID
     var Request = MessageStore.XMLMessages.SingleOrDefault(X => X.Id == RequestId);                  

     // Update the row with the ID of the associated response message
     if (Request != null)
     {
          Request.LinkedMessageId = ResponseId;
          MessageStore.SubmitChanges();
     }
}

但是,SubmitChanges 调用会引发异常:

SQL Server does not handle comparison of NText, Text, Xml, or Image data types.

看起来,SQL 正在将更新消息中的 XML 数据与原始数据进行比较,但我不需要。有没有办法只更新行中的其他列而无需 SQL 绑定来比较 XML 数据?我尝试使用匿名类型,但这些类型是只读的,因此我无法更新值然后存储它。

任何建议将不胜感激。

【问题讨论】:

    标签: xml linq-to-sql


    【解决方案1】:

    在发布问题大约 5 分钟后找到答案!诀窍是在关联属性上将 UpdateCheck 设置为 Never

    [Column(DbType = "Xml NOT NULL", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
    public XElement MessageText { get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多