MySql.EntityFrameworkCore 在 SaveChanges 时生成无效的 SQL 语法。

huangapple go评论53阅读模式
英文:

MySql.EntityFrameworkCore generating invalid SQL syntax on SaveChanges

问题

我正在尝试使用.NET 7和EntityFramework与MySQL。

我使用MySql.EntityFrameworkCore v7.0.0通过NUGET安装。

我能够生成我的上下文和实体(“反向工程/数据库优先”)。我也可以成功地使用生成的DbContext并查询实体。

当我尝试更新实体时,调用SaveChanges() 时出现MySqlException

MySqlException:您的SQL语法有错误;请检查手册,以获取与您的MySQL服务器版本对应的正确语法,以使用RETURNING

我进行了一些谷歌搜索,我可以看到关键字RETURNING在MySQL中不是有效的语法(但是在Oracle和PostGres中是有效的语法,这很有趣,也很奇怪)。

为此UPDATE生成的完整SQL(相当于一个调用中的两个SQL语句)如下:

UPDATE `state_agency` SET `abbreviation` = @p0
WHERE `code` = @p1
RETURNING `row_updated_time`;
SELECT `row_updated_time`
FROM `state_agency`
WHERE ROW_COUNT() = 1 AND `code` = @p1;

这个问题是否对任何人来说很熟悉?

您能否建议我为什么会看到由MySQL提供程序生成的Oracle/Postgres语法的可能原因?

我的直觉告诉我,这可能是某种配置问题,而Oracle语法是从不同的提供程序而不是MySQL提供程序中生成的。

英文:

I am trying to use .NET 7 and EntityFramework with MySQL.

I am using MySql.EntityFrameworkCore v7.0.0 installed via NUGET.

I am able to scaffold my context and entities ("reverse engineer / db first"). Also I can successfully use the generated DbContext and query the entities.

When I attempt to do an update to an entity I get a MySqlException on calling SaveChanges().

> MySqlException: You have an error in your SQL syntax; check the manual
> that corresponds to your MySQL server version for the right syntax to
> use near 'RETURNING row_updated_time;

I did some Google searches and I can see that the keyword RETURNING is not valid syntax in MySql (but it is valid syntax in Oracle and PostGres -- this is interesting, and odd).

The complete SQL that is generated for this UPDATE (amounting to two SQL statements in one call):

UPDATE `state_agency` SET `abbreviation` = @p0
WHERE `code` = @p1
RETURNING `row_updated_time`;
SELECT `row_updated_time`
FROM `state_agency`
WHERE ROW_COUNT() = 1 AND `code` = @p1;

Is this problem familiar to anyone?

Can you suggest a possible reason why I am seeing Oracle/Postgres syntax get generated by the MySql provider?

My gut tells me that there is some sort of configuration problem and the Oracle syntax is coming out of a different provider, rather than the MySql provider.

答案1

得分: 1

I have hit this same issue. We are upgrading a project from .Net 3.1 to .Net 7 and upgrading all of the packages along with it, and after the upgrade we started hitting this issue. We are using Oracle's MySql.EntityFrameworkCore 7.0 as our provider.

From my testing, the issue is specifically tied to updating entities that have "update time" fields. I was able to get the project to run and to create and update database entries by removing the ".ValueGeneratedOnAddOrUpdate()" property from all of the update time fields in my context. The other fields come in fine, but unsurprisingly the update time fields stay static.

This is acceptable to us as a temporary workaround and we're still investigating a better fix. Unfortunately we are unable to switch to Pomelo.

英文:

I have hit this same issue. We are upgrading a project from .Net 3.1 to .Net 7 and upgrading all of the packages along with it, and after the upgrade we started hitting this issue. We are using Oracle's MySql.EntityFrameworkCore 7.0 as our provider.

From my testing, the issue is specifically tied to updating entities that have "update time" fields. I was able to get the project to run and to create and update database entries by removing the ".ValueGeneratedOnAddOrUpdate()" property from all of the update time fields in my context. The other fields come in fine, but unsurprisingly the update time fields stay static.

This is acceptable to us as a temporary workaround and we're still investigating a better fix. Unfortunately we are unable to switch to Pomelo.

答案2

得分: 0

I just ran into this exact problem and after some additional research found out the keyword RETURNING is valid mySQL syntax added in version 8.0.21

这个问题恰好也遇到了我,经过一些额外的研究,我发现关键字RETURNING是在MySQL 8.0.21版本中添加的有效语法。

https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-R

This got me headed down a versioning mismatch direction. Eventually I landed here:

这让我开始考虑版本不匹配的问题。最终,我找到了这个网址:

https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql

I figured out in my case the server's version of mySQL was 5.7.29 and I had to specify that.

我发现在我的情况下,MySQL服务器的版本是5.7.29,所以我必须指定这个版本。

var connectionString = "server=localhost;user=root;password=1234;database=ef;";
var serverVersion = new MySqlServerVersion(new Version(5, 7, 29));

// Replace 'YourDbContext' with the name of your own DbContext derived class.
services.AddDbContext(
dbContextOptions => dbContextOptions
.UseMySql(connectionString, serverVersion)
);

英文:

I just ran into this exact problem and after some additional research found out the keyword RETURNING is valid mySQL syntax added in version 8.0.21

https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-R

This got me headed down a versioning mismatch direction. Eventually I landed here:

https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql

I figured out in my case the server's version of mySQL was 5.7.29 and I had to specify that.

var connectionString = "server=localhost;user=root;password=1234;database=ef";
var serverVersion = new MySqlServerVersion(new Version(5, 7, 29));

// Replace 'YourDbContext' with the name of your own DbContext derived class.
services.AddDbContext<YourDbContext>(
        dbContextOptions => dbContextOptions
            .UseMySql(connectionString, serverVersion)                
    );

答案3

得分: 0

使用[DatabaseGenerated(DatabaseGeneratedOption.Computed)]在CreatedAt字段上,更新模型时出现相同问题。

.NET Core 7 + MySQL 8.0.32 + MySql.EntityFrameworkCore 7.0.5

英文:

As @Chris C said, use [DatabaseGenerated(DatabaseGeneratedOption.Computed)] on CreatedAt field got the same issue on updating models.

.NET Core 7 + MySQL 8.0.32 + MySql.EntityFrameworkCore 7.0.5

huangapple
  • 本文由 发表于 2023年2月10日 06:18:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定