【发布时间】:2019-07-19 06:41:51
【问题描述】:
我正在使用第三方托管服务。
他们为我提供了一个 MySQL 数据库和一个用户。
我正在运行这个简单的查询
SELECT * FROM db_a4b20a_reales.properties_residental;
我遇到了一些奇怪的情况,当尝试从 MySQL Workbench 运行此查询时它可以工作,但是当我尝试从我的 .Net Core 2.2 应用程序运行它时,我收到以下错误:
SELECT command denied to user 'a4b20a_reales'@'{the server ip...}' for table 'properties_residental'
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.BufferlessMoveNext(DbContext _, Boolean buffer, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.AsyncSelectEnumerable`2.AsyncSelectEnumerator.MoveNext(CancellationToken cancellationToken)
at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator`2.MoveNextCore(CancellationToken cancellationToken)
at System.Linq.AsyncEnumerable.AsyncIterator`1.MoveNext(CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable`1 source, TAccumulate seed, Func`3 accumulator, Func`2 resultSelector, CancellationToken cancellationToken)
at SouthRealEstate.DAL.RealEstateDbServices.GetAllResidentalPropertiesAsync() in C:\omri\work\projects\gitRepo\RealEstateProfessional\SouthRealEstate.DAL\RealEstateDbServices.cs:line 56
- 我在 MySQL Workbench 和我的应用程序中都使用同一个用户。
- 我在同一主机/同一环境中运行两者。
- 这是我的连接 str
server=...;port=3306;user id=a4b20a_reales; password=***; database=db_a4b20a_reales; pooling=true; CharSet=utf8; Allow User Variables=True; Convert Zero Datetime=True; default command timeout=720 - 我正在使用
MySql.Data.EntityFrameworkCore(8.0.16) - 请不要用
grant permission回答,这不是问题。
更新:
使用MySql.Data.MySqlClient 时它可以工作,似乎问题出在MySql.Data.EntityFrameworkCore(8.0.16) 连接器上
【问题讨论】:
-
您可能连接到错误/不同的数据库(您的原始查询:
db_reales,连接字符串:db_a4b20a_reales)。您的框架可能会或可能不会使用此数据库,具体取决于您实际运行代码的方式。您可以尝试使用SELECT DATABASE()来确定当前数据库。 -
@Solarflare 你把用户和数据库搞混了,查询是正确的,连接字符串没问题,当我使用 mysql.data lib 时它也可以工作
-
我的意思是:
SELECT * FROM db_reales.xxx-> 查询数据库db_reales。password=***; database=db_a4b20a_reales;连接到数据库db_a4b20a_reales。根据您的框架/设置/实际代码,这可能会查询不同的数据库。 -
@Solarflare 错误地输入了问题,已更新。这不是问题,我正在使用 EF .....
-
请发布异常消息的完整堆栈:idownvotedbecau.se/noexceptiondetails。另外,您是否考虑过切换到nuget.org/packages/Pomelo.EntityFrameworkCore.MySql?很多人报告说它比 MySql.Data.EntityFrameworkCore 少错误。
标签: c# mysql entity-framework asp.net-core