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;
标签:层报,SqlSugarClient,repository,记录,class,musicSourceService,仓储,new,public From: https://www.cnblogs.com/guchen33/p/18106238