abp icon indicating copy to clipboard operation
abp copied to clipboard

Volo.abp.Dapper with dapper contrib

Open jacmiller12 opened this issue 6 years ago • 6 comments

Hello,

Can anyone suggest how to use volo.abp.dapper with dapper.contrib?

I tried this

Tried:- 1

  using (var connection = DbConnection)
 {
     connection.Open();
      var identity = connection.Insert(input);
 }

Tried:- 2 var identity = DbConnection.Insert(input);

Error in Tried:- 1

Error message: Cannot Open when State is Open. StackTrace:-


   at MySql.Data.MySqlClient.MySqlConnection.<OpenAsync>d__26.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 290
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at MySql.Data.MySqlClient.MySqlConnection.Open() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 280

Error in Tried:- 2

Error Message: The transaction associated with this command is not the connection's active transaction; see https://fl.vu/mysql-trans

StackTrace:


   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.MySqlCommand.<ExecuteNonQueryAsync>d__63.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 226
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 74
   at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader) in /_/Dapper/SqlMapper.cs:line 2797
   at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in /_/Dapper/SqlMapper.cs:line 568
   at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in /_/Dapper/SqlMapper.cs:line 441
   at MySqlAdapter.Insert(IDbConnection connection, IDbTransaction transaction, Nullable`1 commandTimeout, String tableName, String columnList, String parameterList, IEnumerable`1 keyProperties, Object entityToInsert) in /_/Dapper.Contrib/SqlMapperExtensions.cs:line 933
   at Dapper.Contrib.Extensions.SqlMapperExtensions.Insert[T](IDbConnection connection, T entityToInsert, IDbTransaction transaction, Nullable`1 commandTimeout) in /_/Dapper.Contrib/SqlMapperExtensions.cs:line 375

jacmiller12 avatar Jul 24 '20 13:07 jacmiller12

The solution is already in the link, see https://fl.vu/mysql-trans.

realLiangshiwei avatar Jul 24 '20 13:07 realLiangshiwei

Hi, @liangshiw I checked this example but I need a connection string from where should I get? I can use the connection string which is in-app settings but when tenant change connection string will change. so I need to implement the logic to fetch the connection string tenant wise and use the same?

Any other idea you have? @liangshiw

jacmiller12 avatar Jul 24 '20 14:07 jacmiller12

You don't need a connection string, because abp will create a connection. Please see the document

realLiangshiwei avatar Jul 24 '20 14:07 realLiangshiwei

Hi @liangshiw ,

I know ABP provides the connection but I am getting error. _Cannot Open when State is Open._ See my implementation.

public class ABCRepository : DapperRepository<MMSDbContext>, ITransientDependency
{
    private readonly IGuidGenerator _guid;
    private readonly IConfiguration _configuration;
    public CEditionTenantRepository(IDbContextProvider<MMSDbContext> dbContextProvider)
  : base(dbContextProvider)
    {

    }

    public void AddTenantEdition(Guid tenantId, Guid editionId)
    {
        try
        {
            using (var connection = DbConnection)
            {
                connection.open();
                var identity = DbConnection.Insert(input);
            }
        }
        catch (Exception ex)
        {

        }

    }
}

jacmiller12 avatar Jul 24 '20 14:07 jacmiller12

Try

     var identity = DbConnection.Insert(input, transaction: DbTransaction);

Or adding IgnoreCommandTransaction=true to your connection string

realLiangshiwei avatar Jul 24 '20 14:07 realLiangshiwei

Thanks @liangshiw ,

 var identity = DbConnection.Insert(input, transaction: DbTransaction);

this one is working perfectly but now dapper.contrib is not supporting _guid_ datatype so I will use ef core now instead of dapper sadly.

jacmiller12 avatar Jul 24 '20 18:07 jacmiller12