问题
On asp.net core application when i execute insert operating application throws error. previously application was asp.net core 1.1 recently i updated to asp.net core 2.0
using (var con = _connectionFactory.CreateConnection())
{
con.Open();
using (var transaction = con.BeginTransaction())
{
try
{
if (regions.Any())
{
con.Insert(regions.Select(c =>
{
c.UserId = userId;
return c;
}), transaction);
}
transaction.Commit();
}
catch(Exception exception)
{
transaction.Rollback();
}
}
}
exception was
MySql.Data.MySqlClient.MySqlException (0x80004005): 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 '
1s () values ()' at line 1 at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in C:\projects\dapper\Dapper\SqlMapper.cs:line 552 at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable
1 commandTimeout, Nullable1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 443 at MySqlAdapter.Insert(IDbConnection connection, IDbTransaction transaction, Nullable
1 commandTimeout, String tableName, String columnList, String parameterList, IEnumerable1 keyProperties, Object entityToInsert) in C:\projects\dapper\Dapper.Contrib\SqlMapperExtensions.cs:line 897
1 commandTimeout) in C:\projects\dapper\Dapper.Contrib\SqlMapperExtensions.cs:line 355
at Dapper.Contrib.Extensions.SqlMapperExtensions.Insert[T](IDbConnection connection, T entityToInsert, IDbTransaction transaction, Nullable
at RecruiterinsiderPortal.Data.AgencyRegionRepository.Save(Int32 userId, List`1 regions) in C:\Empite\RI\RecruiterinsiderPortal\RecruiterinsiderPortal\libs\RecruiterinsiderPortal.Data\AgencyRegionRepository.cs:line 67
mysql version is 5.7.19
what was the issue?
UPDATE
here is the region model
[Table("regions")]
public class AgencyRegion
{
[Key]
public int Id { get; set; }
public int UserId { get; set; }
public int RegionId { get; set; }
}
UPDATE 2
here is the problem i found. i;m gonna open issue on dapper repository when i update dapper and contrib libs in to latest which is 1.50.4 Insert, Update throws exception. so i downgraded to Dapper in to 1.50.2 and Contrib in to 1.50.0 then works fine.
来源:https://stackoverflow.com/questions/48640896/dapper-contrib-insert-exception