Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL + Dapper Extensions: Error in SQL syntax

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?

like image 698
t. YILMAZ Avatar asked Dec 07 '25 04:12

t. YILMAZ


1 Answers

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.

like image 110
Amit Joshi Avatar answered Dec 11 '25 06:12

Amit Joshi