首页 > 解决方案 > Pomelo 使用 MySQL 和 .NET Core 生成的 SQL 不正确

问题描述

我一直在使用 .NET Core 和 Pomelo.EntityFrameworkCore.MySql(v1.1.2,它不是最新的,但出于各种原因我更愿意继续使用它)。

我正在对模型类进行一些重构,创建子父关系(例如,订单有一个客户),现在当我运行我的项目时,我得到了一个异常

2019-09-16T12:36:49.8158085-07:00 0HLPQRKH27D40 [INF] Executed DbCommand (6ms) [Parameters=[], 

CommandType='Text', CommandTimeout='0']
INSERT INTO `Order`
DEFAULT VALUES;
SELECT LAST_INSERT_ID(); (6438bdd5)
2019-09-16T12:36:49.9249074-07:00 0HLPQRKH27D40 [ERR] An exception occurred in the database while saving changes.
 (3b5ca34b)
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> MySql.Data.MySqlClient.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 'DEFAULT VALUES;

问题似乎是 in 的使用,default values因为 insert into 'order' default values;MySQL不支持该语法。我的 mysql 开发版本是 5.1,但这似乎无关紧要,因为它似乎不在更高版本中。

我(天真地)查看了 Pomelo 源代码,但只在单元测试中发现了“默认值”的用法。

标签: c#mysql.net-coreentity-framework-core

解决方案


我正在按顺序将属性映射到另一个对象的属性,例如

public string PropA => obj.ProbA 

当我停止这样做并使用 getter 和 setter 将其更改为长格式时,它开始工作。


推荐阅读