为了账号安全,请及时绑定邮箱和手机立即绑定

.NetCore在运用EF出现错误

.NetCore在运用EF出现错误

波斯汪 2018-12-06 14:44:46
错误信息: 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(); } } 运行之后就出现这个错误,一直没有解决,哪位遇到过请讲解哈,谢谢!
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

services.AddEntityFrameworkSqlServer() .AddDbContext(options => options.UseSqlServer(Configuration["sqlserver"]) );  
查看完整回答
反对 回复 2018-12-09
  • 1 回答
  • 0 关注
  • 2090 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信