错误信息:
No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.
构造的Context
public class DbContextHelper : DbContext
{
public DbSet StudentEntity { get; set; }
public DbContextHelper() { }
public DbContextHelper(DbContextOptions options) : base(options)
{
}
}
接口和服务
public interface IStudentService
{
Task AddStudnet(Student entity);
Task DeltStudent(Guid id);
List GetStudent(Expression> fun);
}
public class StudentService : IStudentService
{
public async Task AddStudnet(Student entity)
{
using (DbContextHelper dbHelper =new DbContextHelper () ) {
await dbHelper.AddAsync(entity);
return dbHelper.SaveChanges();
}
}
public async Task DeltStudent(Guid id)
{
using (DbContextHelper dbHelper =new DbHelper.DbContextHelper ()) {
var entity =await dbHelper.StudentEntity.FindAsync(id);
if (entity == null)
{
return -1;
}
else
{
dbHelper.StudentEntity.Remove(entity);
return dbHelper.SaveChanges();
}
}
}
public List GetStudent(Expression> fun)
{
using (DbContextHelper dbHelper =new DbHelper.DbContextHelper ()) {
var List = dbHelper.StudentEntity.Where(fun).ToList();
return List;
}
}
}
字符串链接配置
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext(options => options.UseSqlite(Configuration["AppSettings:DefaultConnectionStr"]));
services.AddMvc();
services.AddTransient();
}
"AppSettings": {
"DefaultConnectionStr": "data source=.; Initial Catalog=NetCore_TestDB ; uid=sa; pwd=qwertyuiop"
}
使用地方
public class HomeController : Controller
{
private IStudentService StudentDb = new StudentService();
// GET: //
public IActionResult Index()
{
var list = StudentDb.GetStudent(it=>true);
return View();
}
}
运行之后就出现这个错误,一直没有解决,哪位遇到过请讲解哈,谢谢!