簡體   English   中英

PostgreSQL提供程序向Entity Framework for Contains和Concat生成的查詢錯誤

[英]Wrong query generated by PostgreSQL provider to Entity Framework for Contains and Concat

我正在使用EF連接到PostgreSQL。 EF是5.0版,Npgsql是2.0.14.3。

我要運行的查詢是:

var list = new List<string> { "test" };

var res = from t in db.Test
    where !list.Contains(string.Concat(t.test1, "_", t.test2))
    select t;
res.ToList();

生成的查詢類似於:

SELECT "Extent1"."id" AS "id","Extent1"."test1" AS "test1","Extent1"."test2" AS "test2" FROM "public"."test" AS "Extent1" WHERE 'test'!="Extent1"."test1" || '_' || "Extent1"."test2"

當我運行它時,我得到一個錯誤: argument of WHERE must be type boolean, not type text

但是,當我將!=更改為=它可以工作。 當我在Postgres中的連接字符串周圍添加一個括號,但無法更改EF生成的查詢時,它也起作用。

解決方法是,我將第二個(虛擬)元素添加到列表中,因為隨后EF生成了一些不同的查詢,但這不是一個很好的解決方案……

這是pgSQL中的錯誤嗎? 還是在EF提供程序中? 您能為這個問題提出更好的解決方案嗎?

更多信息

表腳本:

CREATE TABLE test
(
  test1 character varying(255),
  test2 character varying(255),
  id integer NOT NULL,
  CONSTRAINT "PK" PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);

該表的類:

[Table("test", Schema = "public")]
public class Test
{
    [Key]
    public int id { get; set; }

    public string test1 { get; set; }

    public string test2 { get; set; }
}

App.config中:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <connectionStrings>
    <add name="local" connectionString="Server=localhost;Port=5432;Database=test;User Id=user;Password=password;" providerName="Npgsql" />
  </connectionStrings>
  <system.data>
    <DbProviderFactories>
      <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>

是的,那是EF提供程序Npgsql中的錯誤。

http://www.postgresql.org/docs/9.3/static/sql-syntax-lexical.html#SQL-PRECEDENCE-TABLE!=|| (any other) ,這意味着它們具有相同的優先級。 它們也保持關聯,因此查詢將被解析為

SELECT ... FROM "public"."test" AS "Extent1"
WHERE ((('test'!="Extent1"."test1") || '_') || "Extent1"."test2")

這是錯誤的。 正確的解析是

SELECT ... FROM "public"."test" AS "Extent1"
WHERE ('test'!=(("Extent1"."test1" || '_') || "Extent1"."test2"))

=優先級比!=低,因此可以使用。

此請求的修復程序包含在https://github.com/npgsql/Npgsql/pull/256的拉取請求中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM