记录一次在设计服务层和仓储层报错
1、首先,使用SqlSugar.IOC连接
SugarIocServices.AddSqlSugar(
new IocConfig()
{
ConnectionString=GetConnectionObject(),
DbType = IocDbType.SqlServer,
IsAutoCloseConnection = true
}
);
2、在仓储层获取DB实例
public class BaseRepository<TEntity> : SimpleClient<TEntity>, IBaseRepository<TEntity> where TEntity : class, new()
{
public BaseRepository()
{
base.Context = DbScoped.Sugar;
}
}
3、写出服务类
public interface IMusicSourceService : IBaseService<MusicSourceInfo>
{
}
public class MusicSourceService : BaseService<MusicSourceInfo>, IMusicSourceService
{
private readonly IBaseRepository<MusicSourceInfo> _repository;
public MusicSourceService(IBaseRepository<MusicSourceInfo> repository):base(repository)
{
_repository = repository;
}
}
4、去做查询
public class HomeViewModel:BindableBase
{
IMusicSourceService _musicSourceService;
public HomeViewModel(IMusicSourceService musicSourceService)
{
_musicSourceService = musicSourceService;
InitCommand = new DelegateCommand(async () => await ExecuteInit());
}
private async Task ExecuteInit()
{
await _musicSourceService.QueryListAsync();
}
public ICommand InitCommand { get; set; }
}
这个时候会出现报错
1、There is already an open DataReader associated with this Connection which must be closed first.
或者
2、Invalid attempt to call FieldCount when reader is closed
或者
3、The connection does not support MultipleActiveResultSets
1、解决方案将SqlSugarClient改为SqlSugarScope ||关闭IsAutoCloseConnection = false
2、解决方案将SqlSugarClient替换成SqlSugarScope ||每次都new一个实例
Context = new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.SqlServer,
ConnectionString = GetConnectionObject(),
IsAutoCloseConnection = true,
});
3、解决方案将SqlSugarClient替换成SqlSugarScope || GetConnectionObject时将MultipleActiveResultSets=True;
本文作者:孤沉
本文链接:https://www.cnblogs.com/guchen33/p/18106238
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
SqlSugar
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步