Linq Any()抛出错误:String未被识别为有效的布尔值

时间:2016-10-31 10:31:38

标签: c# mysql linq asp.net-core entity-framework-core

我正在使用核心MVC构建ASP.NET核心应用程序。由于我打算在Linux环境中托管它,我决定使用MySQL数据库。我已设法设置数据库连接和整个ApplicationDBContext。 LINQ数据库查询工作正常,直到使用Any()查询。 然后发生以下异常:

  

发生了'System.FormatException'类型的异常   Microsoft.EntityFrameworkCore.dll但未在用户代码中处理

     

附加信息:字符串未被识别为有效的布尔值。

这是一个示例代码。从Startup.cs的Configure方法调用此方法:

public async static void Initialize(IApplicationBuilder app)
{
        var services = app.ApplicationServices;
        var dbContext = services.GetRequiredService<ApplicationDbContext>();

        var countResult = (dbContext.Users.Count() > 0); // true
        var anyresult = dbContext.Users.Any(); // exception
}

自动翻译的SQL查询如下所示:

SELECT CASE
    WHEN EXISTS (
        SELECT 1
        FROM `AspNetUsers` AS `a`)
    THEN ('1') ELSE ('0')
END

返回'1'或'0'并且系统尝试将此类型的字符串转换为布尔值,但它失败并抛出上述异常。 我试图通过不使用Any()来解决它,但后来我发现一些内置函数也得到了这个例外,例如: userManager.AddToRolesAsync

这是我的ConfigureServices方法:

public void ConfigureServices(IServiceCollection services)
{
    var sqlConnectionString = Configuration.GetConnectionString("DataAccessMySqlProvider");

    // Add EF services to the services container.
    services
        .AddDbContext<ApplicationDbContext>(options =>
            options.UseMySQL(
                sqlConnectionString,
                b => b.MigrationsAssembly("MyAssembly")
            )
        );

    services.AddIdentity<User, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    // Add framework services.
    services.AddMvc();
}

有什么方法可以解决这个问题吗?

这是我的 project.json 文件:

"dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    "Microsoft.EntityFrameworkCore": "1.0.1",
    "Microsoft.AspNetCore.Identity": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
    "SapientGuardian.EntityFrameworkCore.MySql": "7.1.9"
  },
  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },
  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },
  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  }

1 个答案:

答案 0 :(得分:0)

我将软件包 SapientGuardian.EntityFrameworkCore.MySql 升级到版本7.1.10,其中作者修复了布尔转换问题并开始工作。