误导性SQL异常文本无法进行比较

时间:2016-09-29 15:09:11

标签: servicestack ormlite-servicestack

当OrmLite进行以下调用时,我得到了该异常:

return db.Select<T>(x => x.Name == name && x.PuId == puId).FirstOrDefault();
  

异常:&#34; System.Data.SqlClient.SqlException(0x80131904):文字,   ntext和图像数据类型无法比较或排序,除非   usingIS NULL或LIKE运算符。

名称是String,puid是Int。该类型映射到SQL表,该表没有Text,NText或image类型的列。

当我查看LastSQLStatement并从SQL Server执行它时,它可以工作。当我用以下内容替换呼叫时,它也可以正常工作

return db.SqlList<T>("SELECT Event_Id, Event_Num, Entry_On, Timestamp, Applied_Product, Source_Event, Event_Status, Confirmed, User_Id, Extended_Info, Comment_Id, PU_Id FROM Events WHERE ((Event_Num = @Event_Num) AND (PU_Id = @PU_Id))",new {Event_Num= "16J2730", PU_Id=91}).FirstOrDefault();

我的服务的旧版本可以使用相同的代码正常工作。使用最新版本的servicestack和ormlite,我现在遇到了这个奇怪的问题......

最新版本的OrmLite是否存在旧版SQL Server的问题?我们仍然是2000版。我没有运气使用SQLServer方言。

有人有想法吗?

以下是Mythz要求的内容

        public ProficyEvent TestGetByName(string name, int puId, bool withDetails = false)
    {
        using (IDbConnection db = OpenDBConnection())
        {
            try
            {                   
                return db.Select<ProficyEvent>(x => x.Name == name && x.PuId == puId).FirstOrDefault();             
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error querying database: {0}", ex.ToString());
                throw;
            }
        }           
    }

[Alias("Events")]
public class ProficyEvent:IProficyPuEntity
{       
    [AutoIncrement]
    [Alias("Event_Id")]
    public int Id { get; set; }     
    [Ignore]
    public string Code { get; set; }
    [Ignore]
    public string Desc { get; set; }
    [Alias("Event_Num")]
    public string Name { get; set; }
    [Alias("Entry_On")]
    public DateTime? LastModified { get; set; }
    [Ignore]
    public string LastModifiedBy { get; set; }      
    public DateTime? Timestamp { get; set; }
    [Alias("Applied_Product")]
    public int? AppliedProductId { get; set; }
    [Ignore]
    public string AppliedProductName { get; set; }
    [Ignore]
    public int OriginalProductId { get; set; }
    [Ignore]
    public string OriginalProductName { get; set; }
    [Alias("Source_Event")]
    public int? SourceEvent { get; set; }
    [Alias("Event_Status")]
    public int? EventStatus { get; set; }
    [Ignore]
    public string EventStatusName { get; set; }
    public int Confirmed { get; set; }
    [Alias("User_Id")]
    public int UserId { get; set; }
    [Alias("Extended_Info")]
    public string ExtendedInfo { get; set; }
    [Ignore]
    public string Comment { get; set; }
    [Alias("Comment_Id")]
    public int? CommentId { get; set; }     
    [Ignore]
    public IEnumerable<ProficyTest> TestResults { get; set; }
    [Alias("PU_Id")]
    public int PuId { get; set; }
    [Ignore]
    public string UnitName { get; set; }
    [Ignore]
    public string LineName { get; set; }
}

CREATE TABLE [dbo].[Events](
[Event_Id] [int] IDENTITY(1,1) NOT NULL,
[Event_Num] [Varchar_Event_Number] NOT NULL,
[PU_Id] [int] NOT NULL,
[TimeStamp] [datetime] NOT NULL,
[Applied_Product] [int] NULL,
[Source_Event] [int] NULL,
[Event_Status] [tinyint] NULL,
[Confirmed] [bit] NOT NULL DEFAULT (0),
[User_Id] [int] NULL,
[Comment_Id] [int] NULL,
[Entry_On] [datetime] NULL,
[Testing_Status] [int] NULL DEFAULT (1),
[Event_Subtype_Id] [int] NULL,
[Start_Time] [Datetime_ComX] NULL,
[Extended_Info] [varchar](255) NULL,
[Converted_Timestamp] [datetime] NULL,
[Orientation_X] [float] NULL,
[Orientation_Y] [float] NULL,
[Orientation_Z] [float] NULL,
[Final_Dimension_Z] [real] NULL,
[Final_Dimension_A] [real] NULL,
[Initial_Dimension_A] [real] NULL,
[Final_Dimension_X] [real] NULL,
[Final_Dimension_Y] [real] NULL,
[Initial_Dimension_Y] [real] NULL,
[Initial_Dimension_Z] [real] NULL,
[Initial_Dimension_X] [real] NULL,
[Conformance] [tinyint] NULL,
[Testing_Prct_Complete] [tinyint] NULL)

CREATE TYPE [dbo].[Varchar_Event_Number] FROM [varchar](25) NOT NULL
CREATE TYPE [dbo].[Datetime_ComX] FROM [datetime] NOT NULL

1 个答案:

答案 0 :(得分:1)

要回答这个问题,这在最新版本的SQL Server上运行没有问题。

可能会影响此行为的OrmLite的主要更改是从内联SQL参数中使用Parameterized SQL Expressions的更改。

您可以使用OrmLite's legacy APIs使用内联SQL参数,也可以使用Custom SQL APIs下拉。