EF6 for MySql生成不需要的`[]`转义字符

时间:2016-05-23 14:42:19

标签: mysql entity-framework

我的应用程序通过切换Web.config中的配置在SQL Server和Mysql上运行。

我刚刚添加了一个新实体并在SQL Server中使用它。好的,好的,让我们改变。

所有简单的get语句都可以正常工作,因此我可以浏览页面。但是,当我点击某些 Lambda查询时,我会收到意外且不需要的错误

        using (auitool2014Entities dataContext = GetDataContext())
        {
            return dataContext.sezione_a.Any(a =>
                a.utente == utenteId &&
                startDateString.CompareTo(a.A21) <= 0 &&
                endDateString.CompareTo(a.A21) >= 0
                );
        }

基本上我需要检查是否存在至少一个具有gien约束的行(是的,那些日期字段存储为yyyyMMdd字符串,因此我将对它们进行比较。)

我得到的错误是因为EF6生成方括号来转义表名。奇怪的。很奇怪,因为当我用断点点击实体集合时,我得到以下内容

{SELECT
`Extent1`.`A01`, 
`Extent1`.`A01a`, 
`Extent1`.`A01b`, 
`Extent1`.`A02`, 
`Extent1`.`A03`, 
`Extent1`.`A11`, 
`Extent1`.`A12`, 
`Extent1`.`A12a`, 
`Extent1`.`A12b`, 
`Extent1`.`A12c`, 
`Extent1`.`A21`, 
`Extent1`.`A22`, 
`Extent1`.`A23`, 
`Extent1`.`A24`, 
`Extent1`.`A25`, 
`Extent1`.`A31`, 
`Extent1`.`A31a`, 
`Extent1`.`A31b`, 
`Extent1`.`A32`, 
`Extent1`.`A33`, 
`Extent1`.`A33a`, 
`Extent1`.`A33b`, 
`Extent1`.`A33c`, 
`Extent1`.`A34`, 
`Extent1`.`A41`, 
`Extent1`.`A42`, 
`Extent1`.`A43`, 
`Extent1`.`A51`, 
`Extent1`.`A52`, 
`Extent1`.`A53`, 
`Extent1`.`A54`, 
`Extent1`.`A54a`, 
`Extent1`.`A54b`, 
`Extent1`.`A54c`, 
`Extent1`.`B11`, 
`Extent1`.`B12`, 
`Extent1`.`B13`, 
`Extent1`.`B14`, 
`Extent1`.`B15`, 
`Extent1`.`Z0`, 
`Extent1`.`prog`, 
`Extent1`.`utente`
FROM (SELECT 
    [sezione_a].[A01] AS [A01], 
    [sezione_a].[A01a] AS [A01a], 
    [sezione_a].[A01b] AS [A01b], 
    [sezione_a].[A02] AS [A02], 
    [sezione_a].[A03] AS [A03], 
    [sezione_a].[A11] AS [A11], 
    [sezione_a].[A12] AS [A12], 
    [sezione_a].[A12a] AS [A12a], 
    [sezione_a].[A12b] AS [A12b], 
    [sezione_a].[A12c] AS [A12c], 
    [sezione_a].[A21] AS [A21], 
    [sezione_a].[A22] AS [A22], 
    [sezione_a].[A23] AS [A23], 
    [sezione_a].[A24] AS [A24], 
    [sezione_a].[A25] AS [A25], 
    [sezione_a].[A31] AS [A31], 
    [sezione_a].[A31a] AS [A31a], 
    [sezione_a].[A31b] AS [A31b], 
    [sezione_a].[A32] AS [A32], 
    [sezione_a].[A33] AS [A33], 
    [sezione_a].[A33a] AS [A33a], 
    [sezione_a].[A33b] AS [A33b], 
    [sezione_a].[A33c] AS [A33c], 
    [sezione_a].[A34] AS [A34], 
    [sezione_a].[A41] AS [A41], 
    [sezione_a].[A42] AS [A42], 
    [sezione_a].[A43] AS [A43], 
    [sezione_a].[A51] AS [A51], 
    [sezione_a].[A52] AS [A52], 
    [sezione_a].[A53] AS [A53], 
    [sezione_a].[A54] AS [A54], 
    [sezione_a].[A54a] AS [A54a], 
    [sezione_a].[A54b] AS [A54b], 
    [sezione_a].[A54c] AS [A54c], 
    [sezione_a].[B11] AS [B11], 
    [sezione_a].[B12] AS [B12], 
    [sezione_a].[B13] AS [B13], 
    [sezione_a].[B14] AS [B14], 
    [sezione_a].[B15] AS [B15], 
    [sezione_a].[Z0] AS [Z0], 
    [sezione_a].[prog] AS [prog], 
    [sezione_a].[utente] AS [utente]
    FROM [dbo].[sezione_a] AS [sezione_a]) AS `Extent1`}

第一部分是合理正确的,但第二个id奇怪地来自SQL Server !!!

以下是我的配置:

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </providers>
  </entityFramework>

如何解决问题?

1 个答案:

答案 0 :(得分:0)

问题的原因是,由于某种原因,EF将我的实体sezione_a映射到其相应的表,而不映射到该表上的预定义查询。

也许是因为在某些时候我错过了主键。