gpt4 book ai didi

c# - Linq Any() 抛出错误 : String was not recognized as a valid boolean

转载 作者:行者123 更新时间:2023-11-30 22:10:08 25 4
gpt4 key购买 nike

我正在使用核心 MVC 构建一个 ASP.NET 核心应用程序。因为我打算在 Linux 环境中托管它,所以我决定使用 MySQL 数据库。我设法设置了数据库连接和整个 ApplicationDBContext。 LINQ DB 查询工作正常,直到使用 Any() 查询的点。然后出现如下异常:

An exception of type 'System.FormatException' occurred inMicrosoft.EntityFrameworkCore.dll but was not handled in user code

Additional information: String was not recognized as a valid Boolean.

这是一个示例代码。这个方法是从 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”,系统会尝试将这种类型的字符串转换为 bool 值,但失败并抛出上述异常。我试图通过根本不使用 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
}
}

最佳答案

我将包 SapientGuardian.EntityFrameworkCore.MySql 升级到版本 7.1.10,作者修复了 bool 转换的问题并开始工作。

关于c# - Linq Any() 抛出错误 : String was not recognized as a valid boolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40340394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com