0

我正在使用 .Net Framework 4 和 MySQL 开发应用程序。由于 ORM 实体框架与 6.6.4 版本的 MySQL 连接器一起用作数据提供者。

尝试执行以下代码片段时抛出 NotSupportedException。

using (var connection = new Connection())
{
    var commandTxt = 
@"SELECT
    t.ID,
    ANYELEMENT
    (
        SELECT VALUE TOP(1) c.Name
        FROM Connection.Contacts AS c
        WHERE c.PartyID = t.ID
    ) AS SubQuery
FROM Connection.Parties AS t";
    var q = new ObjectQuery<DbDataRecord>(commandTxt, connection);
    var result = q.ToList();
}

例外:

System.Data.EntityCommandCompilationException: An error occurred while preparing the command definition. See the inner exception for details. ---> System.NotSupportedException: Specified method is not supported.
   at MySql.Data.Entity.SqlGenerator.Visit(DbApplyExpression expression)
   at System.Data.Common.CommandTrees.DbApplyExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type)
   at MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type)
   at MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression)
   at System.Data.Common.CommandTrees.DbProjectExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree)
   at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
   at System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree)
   --- End of inner exception stack trace ---
   at System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree)
   at System.Data.EntityClient.EntityProviderServices.CreateCommandDefinition(DbProviderFactory storeProviderFactory, DbCommandTree commandTree)
   at System.Data.EntityClient.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
   at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Span span, ReadOnlyCollection`1 compiledQueryParameters, AliasGenerator aliasGenerator)
   at System.Data.Objects.EntitySqlQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery.ToTraceString()
   at WpfApplication1.MainWindow.ExecuteQuery(String txtParam, String& query, String& result) in c:\Users\andrienko.EDSSON\Documents\Visual Studio 2012\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:line 96

数据模型:

CREATE DATABASE `test_schema` /*!40100 DEFAULT CHARACTER SET latin1 */;

CREATE TABLE `party` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ID`)
);

CREATE TABLE `contact` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `PartyID` int(11) NOT NULL,
  `Name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ID`,`PartyID`),
  KEY `fk_contact_party_idx` (`PartyID`),
  CONSTRAINT `fk_contact_party` FOREIGN KEY (`PartyID`) REFERENCES `party` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
);

此行为只能在安装了 .Net Framework 4.5 的机器上重现(无需对解决方案设置进行任何更改),但在仅安装了 .Net Framework 4 的机器上可以正常工作。似乎 .Net Framework 4.5 安装替换了 System.Data.dll 库。

有人可以建议任何解决方法吗?

4

0 回答 0