我試圖改變的方法,使之異步和使用小巧玲瓏 這是一個repository類的當前方法:無法轉換使用toList()在異步方法
...
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();
}
}
我想使其異步這樣的:
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();
});
}
不幸的是,我得到以下錯誤:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)
w ^我需要改變爲像第一種方法返回?
附加信息: 這是我敢繼承一個倉庫的基類:
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);
}
}
}
}
嗯......我改爲toListAsync()方法,因爲你建議,但是...不可用,但... VS建議我添加EntityFramework軟件包。不知道該怎麼做,因爲我使用的是Dapper,而不是EF。 –
是的,沒有看到...評論刪除 – ghg565