【问题标题】:SubSonic3 SetExpression problemSubSonic3 SetExpression 问题
【发布时间】:2011-01-11 11:55:20
【问题描述】:

下一个 SubSonic3 查询给我一个错误:

Db.Update<Tag>()
    .SetExpression("Popularity")
    .EqualTo("Popularity+1")
    .Where<Tag>(x => x.TagId == tagId)
    .Execute();

错误:失败:System.FormatException:无法将参数值从字符串转换为 Int32。

生成的sql是ok的,但是参数集合中包含两个需要设置的参数。

UPDATE [Tagging].[Tag] 
SET Popularity=Popularity+1
WHERE [Tagging].[Tag].[TagId] = @0

其中一个参数将@up_Popularity 设置为“Popularity+1”。由于这是设置的第一个参数,sql 尝试将此字符串 'Popularity+1' 分配给一个整数。

这是一个错误还是我做错了什么?

【问题讨论】:

    标签: subsonic3


    【解决方案1】:
    Db.Update<Tag>()
        .SetExpression("Popularity = Popularity + 1")
        .Where<Tag>(x => x.TagId == tagId)
        .Execute();
    

    这应该可行……但我认为这是为了批发更新。没有把握。您最好的选择是使用我们的 CodingHorror:

    new CodingHorror("UPDATE Tags SET Popularity = Popularity + 1 WHERE @1", 
      tagId).Execute();
    

    【讨论】:

    • Rob,这似乎引发了一个错误,提示“无法解析符号在哪里”。显然,SetExpression 返回的是 Setting 实例而不是 Update 实例。
    • 这个问题stackoverflow.com/questions/2429381/… 解决了同样的问题,但建议的解决方案也不起作用(然后我的问题中出现上述错误)。
    • CodingHorror 解决方案是唯一的方法吗,Rob?可以修复它以使您的第一个示例正常工作吗?
    【解决方案2】:

    如果这应该起作用,我会感到惊讶。当我需要将字符串作为 SQL 的一部分进行评估时(而不是通过 SubSonic),我几乎总是不得不使用 CodingHorror

    但是,您应该能够通过使用单独的查询来执行此操作。比如:

    Db.Update<Tag>()
      .Set("Popularity")
      .EqualTo(Tag.SingleOrDefault(t => t.TagId == tagId).Popularity + 1)
      .Where<Tag>(x => x.TagId == tagId)
      .Execute();
    

    【讨论】:

    • 嗯,像这样它可能会工作,但你必须去数据库两次。
    • 我认为我的查询应该可以正常工作,但是因为 subsonic 创建了两个 sql 参数,所以它不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多