问题
I am trying to do CRUD operations using Dapper Extensions. But I am getting error while inserting data to MySQL database as follows:
Error: 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 [......] at line [....]
If I use MSSQL database, Dapper Extensions is working correctly. Why I am getting this error with MySQL?
回答1:
The error is because Dapper Extensions is generating the query for SQL server (by default) where as you are actually connected to MySQL. There are syntax differences between these two RDBMSs and hence the error. You have to tell Dapper Extensions that you are connecting to MySQL.
Set the Dialect at the startup of your application somewhere.
//Synchronous
DapperExtensions.DapperExtensions.SqlDialect = new DapperExtensions.Sql.MySqlDialect();
//Asynchronous
DapperExtensions.DapperAsyncExtensions.SqlDialect = new DapperExtensions.Sql.MySqlDialect();
As you can note, you need to configure this separately for synchronous and asynchronous methods. You can read more about this on github.
This will instruct Dapper Extensions to generate the queries according to the syntax of MySql. Not only Dapper Extensions, similar is necessary for many ORMs those support query generation for multiple RDBMSs.
Apart from this, you may also consider implementing logging which may help you diagnose the issues. MiniProfiler is good tool for this purpose. You may find more details in my other answer.
来源:https://stackoverflow.com/questions/53016610/mysql-dapper-extensions-error-in-sql-syntax