Ich versuche, eine Methode zu ändern, es async und mit Dapper Dies ist die aktuelle Methode in einem Repository-Klasse ist zu machen:Kann nicht mit ToList() in Asynchron-Methode konvertieren
...
public class ClientsRepository : BaseRepository
{
public ClientsRepository(string connectionString): base (connectionString){ }
...
public List<ClientSummaryModel> GetClientSummaryCredit(string id)
{
try
{
connection();
con.Open();
con.ChangeDatabase("PAQA");
IList<ClientSummaryModel> SummaryClientList= SqlMapper.Query<ClientSummaryModel>(con, "dbo.SP_GET_CLIENT_SUMMARY '" + id + "','0','CREDIT'", CommandType.StoredProcedure).ToList();
return SummaryClientList.ToList();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
Ich versuche, machen sie es ASync dies mag:
public async Task<List<ClientSummaryModel>> GetClientSummaryCredit(string id)
{
return await WithConnection(async c =>
{
c.ChangeDatabase("PAQA");
IList<ClientSummaryModel> SummaryClientList= await c.QueryAsync<ClientSummaryModel>("dbo.SP_GET_CLIENT_SUMMARY '" + id + "','0','CREDIT'", CommandType.StoredProcedure).ToList();
return SummaryClientList.ToList();
});
}
Leider bekomme ich folgende Fehlermeldung:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)
W muss ich ändern, um wie die erste Methode zurückzukehren?
Zusätzliche Informationen: Hier ist es ein Repository Basisklasse, die ich bin Subklassen:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Dapper;
namespace AutorizarCreditoApp.Repositories
{
public abstract class BaseRepository
{
private readonly string _ConnectionString;
protected BaseRepository(string connectionString)
{
_ConnectionString = connectionString;
}
protected async Task<T> WithConnection<T>(Func<IDbConnection, Task<T>> getData)
{
try
{
using (var connection = new SqlConnection(_ConnectionString))
{
await connection.OpenAsync(); // Asynchronously open a connection to the database
return await getData(connection); // Asynchronously execute getData, which has been passed in as a Func<IDBConnection, Task<T>>
}
}
catch (TimeoutException ex)
{
throw new Exception(String.Format("{0}.WithConnection() experienced a SQL timeout", GetType().FullName), ex);
}
catch (SqlException ex)
{
throw new Exception(String.Format("{0}.WithConnection() experienced a SQL exception (not a timeout)", GetType().FullName), ex);
}
}
}
}
Mmmm ... Ich ändere zu dieser toListAsync() -Methode, wie Sie vorgeschlagen, aber ... ist nicht verfügbar, aber ... VS schlägt mir vor, EntityFramework-Paket hinzuzufügen. Ich bin mir nicht sicher, was ich tun soll, weil ich Dapper und nicht EF verwende. –
yeah, habe das nicht gesehen ... Kommentar gelöscht – ghg565