Using SqlSugar ORM, based on blazor, dependency injection business service, an error is reported when calling。 SqlSugarService:
public static class SqlSugarService
{
private static readonly ILog log = LogManager.GetLogger(typeof(SqlSugarService));
public static void AddSqlSugarSevice(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
services.AddScoped<ISqlSugarClient>(o =>
{
var listConfig = new List<ConnectionConfig>();
listConfig.Add(new ConnectionConfig
{
DbType = DbType.SqlServer,
ConnectionString = "Server=.\\SQLEXPRESS;DataBase=Test;Uid=sa;Pwd=123456",
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
var dbContext = new SqlSugarClient(listConfig);
return dbContext;
});
}
}
the interface:
public interface IReportRepository
{
public DataTable GetTest(string sql);
}
Interface implementation:
public class ReportRepository : IReportRepository
{
private ISqlSugarClient _dbBase;
public ReportRepository(ISqlSugarClient sqlSugar)
{
_dbBase = sqlSugar;
}
public DataTable GetTest(string sql)
{
return _dbBase.Ado.GetDataTable(sql);
}
}
injection:
services.AddSqlSugarSevice();
services.TryAddTransient<IReportRepository, ReportRepository>();
use constructor injection:
private readonly IReportRepository _reportRepository;
//[Inject]
//public IReportRepository ReportService { get; set; }
public Report(IReportRepository reportRepository)
{
_reportRepository = reportRepository;
_reportRepository.GetTest("select * from test");
}
ERROR :
An unhandled exception occurred while processing the request. MissingMethodException: No parameterless constructor defined for type 'MyReport.Pages.Report'. System.RuntimeType.CreateInstanceDefaultCtorSlow(bool publicOnly, bool wrapExceptions, bool fillCache)
public partial class Report { private readonly IReportRepository _reportRepository; //[Inject] //public IReportRepository ReportService { get; set; } public Report() { } public Report(IReportRepository reportRepository) { _reportRepository = reportRepository; _reportRepository.GetTest("select * from test"); } }

