ASP.NET Core-执行命令后找不到密钥 [英] ASP.NET Core - Key Not Found Upon Execute Command

查看:183
本文介绍了ASP.NET Core-执行命令后找不到密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到我的本地数据库-使用ASP.NET Core可以使用localhost/phpmyadmin访问该数据库.我是该语言的新手,并且正在努力解决与MySQL相关的异常.

I'm trying to connect to my local database - which I can access using localhost/phpmyadmin - using ASP.NET Core. I'm new to the language, and I am struggling with a MySQL-related exception.

string constr = Configuration.GetSection("ConnectionStrings:con").Value;
using (MySqlConnection con = new MySqlConnection(constr))
{
    string query = @"SELECT id
                    FROM `test`";
    using (MySqlCommand cmd = new MySqlCommand(query, con))
    {
        con.Open();
        using (var sqlDataReader = cmd.ExecuteReader())
        {
            while (sqlDataReader.Read())
            {
                var id = sqlDataReader.GetInt32(0);
            }
        }
        con.Close();
    }
}

使用(var sqlDataReader = cmd.ExecuteReader())在行发生异常.

The exception happens at the line using (var sqlDataReader = cmd.ExecuteReader()).

这是我的连接字符串:

"ConnectionStrings": {
    "con": "Server=localhost;Database=musense;User=root;Password=********;Charset=utf8"
 }

我正在从appsettings.json中设置连接字符串.如果没有字符集,则会出现一个异常,即'windows-1252'不是受支持的编码名称."

I am setting the connection string from within the appsettings.json. Without the charset, I get an exception that "'windows-1252' is not a supported encoding name."

运行上述代码后,我在行使用(var sqlDataReader = cmd.ExecuteReader())收到以下异常::

Upon running the above code, I get the following exception at the line using (var sqlDataReader = cmd.ExecuteReader()).:

/home/memonick/Documents/Projects/musense/musense.csproj : warning NU1701: Package 'MySql.Data 6.9.9' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
/home/memonick/Documents/Projects/musense/musense.csproj : warning NU1701: Package 'MySql.Data 6.9.9' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.ThrowHelper.ThrowKeyNotFoundException()
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at MySql.Data.MySqlClient.MySqlField.SetFieldEncoding()
   at MySql.Data.MySqlClient.NativeDriver.GetColumnData(MySqlField field)
   at MySql.Data.MySqlClient.NativeDriver.GetColumnsData(MySqlField[] columns)
   at MySql.Data.MySqlClient.Driver.GetColumns(Int32 count)
   at MySql.Data.MySqlClient.ResultSet.LoadColumns(Int32 numCols)
   at MySql.Data.MySqlClient.ResultSet..ctor(Driver d, Int32 statementId, Int32 numCols)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlDataReader.Close()
   at MySql.Data.MySqlClient.MySqlCommand.ResetReader()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at musense.Startup.ConfigureServices(IServiceCollection services) in /home/memonick/Documents/Projects/musense/Startup.cs:line 46
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at musense.Program.BuildWebHost(String[] args) in /home/memonick/Documents/Projects/musense/Program.cs:line 21
   at musense.Program.Main(String[] args) in /home/memonick/Documents/Projects/musense/Program.cs:line 17

值得注意的是,正在建立连接.因此,例如,我可以更新一行,或执行SELECT VERSION().仅当我尝试在ASP.NET Core的表上执行SELECT语句时,才会发生此问题.该语句本身似乎有效,因为它通常在phpmyadmin中执行.

It's worth noting that the connection is being established. Thus, for example, I can update a row, or execute SELECT VERSION(). The problem happens only when I try to execute a SELECT statement on a table from ASP.NET Core. The statement itself seems valid, as it executes normally in phpmyadmin.

测试表具有以下架构:

CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL,
  `value` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;

我尝试了各种排序规则,但异常仍然存在.欢迎任何帮助!

I've tried various collations, but the exception persists. Any help is welcome!

推荐答案

由于使用的NuGet软件包与 .NETCoreApp2.0 不兼容,因此您的问题更加严重:

Your issue rises by the fact that you are using a NuGet package not compatible with .NETCoreApp2.0:

/home/memonick/Documents/Projects/musense/musense.csproj : warning NU1701: Package 'MySql.Data 6.9.9' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

您需要升级 MySql.Data 6.9.9 并至少使用 6.10.3-rc 版本(请记住检查 show prerelease软件包>在nuget浏览器中),与 .netstandard1.3 及更高版本(包括 .NETCoreApp2.0 )兼容.

You need to upgrade MySql.Data 6.9.9 and use at least 6.10.3-rc version (remember to check show prerelease packages in nuget browser), which is compatible with .netstandard1.3 and above (including .NETCoreApp2.0).

这篇关于ASP.NET Core-执行命令后找不到密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆