【问题标题】:ASP MVC MsSql to MySQL migrationASP MVC MsSql 到 MySQL 迁移
【发布时间】:2015-12-23 18:45:20
【问题描述】:

对于低预算项目,我必须使用 MySql 运行 IIS Asp Mvc。迁移现有项目运行良好,但如果我使用 Take & Skip 创建 LINQ 查询,则会失败。

第一次测试(正常)

var post = _db.Posts.FirstOrDefaultAsync(a => a.id == 1234);

第二次测试(正常)

var post = _db.Posts.Include(a => a.Comments);
var result = await post.Select(a => new TRDPostViewModel
{
  Created = a.Created,
  Body = a.Body,
  Comments = a.Comments.Select(d => new TRDCommentViewModel
  {
     Body = d.Body,
     Id = d.Id,
  }).Where(m => m.Trash == false)
     .OrderByDescending(f => f.Created)
   .ToList(),
}).FirstOrDefaultAsync();

第三次测试(失败)

var result = await post.Select(a => new TRDPostViewModel
{
  Created = a.Created,
  Body = a.Body,
  Comments = a.Comments.Select(d => new TRDCommentViewModel
  {
     Body = d.Body,
     Id = d.Id,
  }).Where(m => m.Trash == false)
     .OrderByDescending(f => f.Created)
     .Skip(33)
     .Take(10)
   .ToList(),
}).FirstOrDefaultAsync();

这里是踪迹:

'where 子句'MySql.Data.MySqlClient.MySqlException 中的未知列'Extent1.Id'

完全没有意义。与 MsSql 相同的代码工作正常。使用最新的 MySql.Data.Entity.EF6,版本=6.9.7.0

我错过了什么吗?花费数小时解决但没有成功。

【问题讨论】:

  • 您是否尝试激活 EF 记录器 (Database.Log = Console.Write) 以检查发生了什么?
  • Jap 我记录了失败的 sql 查询。但这真的很奇怪。我搜索日志并将其粘贴到此处。

标签: c# mysql asp.net asp.net-mvc database-migration


【解决方案1】:

你确定你的第二个查询真的没问题吗?

1) Id = d.Id,

2) .Where(m => m.Trash == false)

3) .OrderByDescending(f => f.Created)

4) 为什么在 .ToList() 后面加逗号?

我已经使用生成的数据简化了您的 DDL(不是 MWE)。 我已经在 VS2013 中重现了你的问题。

我还使用 LINQPad 直接针对数据库测试了您的查询,第三次测试也遇到了同样的问题,可能是驱动程序 mysql 中的错误:

trdposts.Select(a => new {
    Created = a.Created,
    Body = a.Body,
    Comments = a.Posttrdcomments
                .Select(d => new { Body = d.body, Id = d.Id, d.Created, d.Trash})
                .Where(m => m.Trash == 1)
                .OrderByDescending(f => f.Created)
                .Skip(33)
                .Take(10)
                .ToList()
    })

给出一个更短的 SQL 查询:

SELECT t1.PostId, t1.body, t1.Id, t1.Created, t1.Trash
FROM trdposts AS t0
    OUTER APPLY (
      SELECT t2.body, t2.Created, t2.Id, t2.PostId, t2.Trash
      FROM trdcomments AS t2
      WHERE ((t2.PostId = t0.Id) AND (t2.Trash = 1))
      ORDER BY t2.Created DESC
  ) AS t1
ORDER BY t1.Created DESC

没有 .Skip() 和 .Take(),我们会得到很好的“左外连接”

【讨论】:

  • 日本确认。忽略逗号 - 它没有任何意义,但在那里没问题。我也认为那是mysql驱动程序中的一个错误。感谢您抽出时间来寻找这个错误。下一步是什么?我尝试联系 MySQL 好友并让您知道。
【解决方案2】:

这种查询对于 MySQL 来说是不可能的。如果您自己编写查询,您将如何用 SQL 编写它?问题是 MySQL 不支持 SQL 标准所谓的横向连接,在 MsSQL 中使用了关键字“APPLY”。 PostgreSQL 和 MsSQL 的 .NET 驱动程序支持这类查询,但不支持 MySQL。

【讨论】:

    【解决方案3】:

    这是 MySQL EF 的一个已知问题,其他帖子可能是 MySQL 本身。 见:

    https://bugs.mysql.com/bug.php?id=78610

    和其他 SO 帖子:

    Unknown column 'Project2.Name' in 'where clause'

    它是在 3 年前发布的,如果它告诉您任何内容,则标记为关键。它存在于 MySQL 连接器 6.9.11.0 及更高版本中。我只需要尽我所能解决它。我不希望它得到修复。

    【讨论】:

    • 感谢您的回答。与此同时,我切换到 MongoDB 作为数据库提供者。 2 年前,我在 oracle bugtracker 数据库中报告了这一点。直到今天,该错误仍然存​​在。
    猜你喜欢
    • 2018-01-07
    • 2016-07-18
    • 2016-06-22
    • 2014-09-05
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    相关资源
    最近更新 更多