首页新闻找找看学习计划

.NetCore在运用EF出现错误

0
[已解决问题] 解决于 2016-11-28 14:10

错误信息:

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<TContext> object in its constructor and passes it to the base constructor for DbContext.

构造的Context

public class DbContextHelper : DbContext
    {
        public DbSet<Student> StudentEntity { get; set; }

        public DbContextHelper() { }

        public DbContextHelper(DbContextOptions options) : base(options)
        {
        }

    }

接口和服务

 public interface IStudentService
    {
        Task<int> AddStudnet(Student entity);

        Task<int> DeltStudent(Guid id);

        List<Student> GetStudent(Expression<Func<Student,bool>> fun);
    }
public class StudentService : IStudentService
    {
        public  async Task<int> AddStudnet(Student entity)
        {
            using (DbContextHelper dbHelper =new DbContextHelper () ) {
                await dbHelper.AddAsync(entity);
                return dbHelper.SaveChanges();
            }
        }

        public async Task<int> 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<Student> GetStudent(Expression<Func<Student, bool>> 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<DbContextHelper>(options => options.UseSqlite(Configuration["AppSettings:DefaultConnectionStr"]));
            services.AddMvc();
            services.AddTransient<IStudentService, StudentService>();
        }
"AppSettings": {
    "DefaultConnectionStr": "data source=.; Initial Catalog=NetCore_TestDB ; uid=sa; pwd=qwertyuiop"
  }

使用地方

 public class HomeController : Controller
    {
        private IStudentService StudentDb = new StudentService();
        // GET: /<controller>/
        public IActionResult Index()
        {
            var  list = StudentDb.GetStudent(it=>true);
            return View();
        }
    }

运行之后就出现这个错误,一直没有解决,哪位遇到过请讲解哈,谢谢!

追随微笑的主页 追随微笑 | 菜鸟二级 | 园豆:217
提问于:2016-11-28 09:58
< >
分享
最佳答案
0
            services.AddEntityFrameworkSqlServer()
                .AddDbContext<AppDbContext>(options =>
                 options.UseSqlServer(Configuration["sqlserver"])
                );

 

奖励园豆:5
吴瑞祥 | 高人七级 |园豆:28770 | 2016-11-28 10:10
 services.AddEntityFrameworkSqlServer()
                .AddDbContext<DbContextHelper>(options =>
                 options.UseSqlServer(Configuration["AppSettings:DefaultConnectionStr"])
                );

添加之后还是这个错误

追随微笑 | 园豆:217 (菜鸟二级) | 2016-11-28 10:28

@追随微笑: 又是这种封装ef的东西...

别封装啦大哥.乖乖从容器取上下文不行吗.偏要搞这些有的没的.

吴瑞祥 | 园豆:28770 (高人七级) | 2016-11-28 10:40

@吴瑞祥: 我没有封装上下文啊,只是把实体类操作封装了哈.

追随微笑 | 园豆:217 (菜鸟二级) | 2016-11-28 10:42

@追随微笑: 唉.别搞这些啦.搞这种不如不用ef.

你的问题处在你怎么获取上下文上.因为你封装了.所以我也不知道你怎么弄的.自然就不知道怎么改.

吴瑞祥 | 园豆:28770 (高人七级) | 2016-11-28 10:56

谢谢,开始问题问题解决了,但是出现一个问题,难道.net Core的链接字符串和一般的EF链接字符串不一样啊,我这样写会报错

 string str = "data source=.;Initial Catalog=NetCore_TestDB;uid=sa; pwd=qwertyuiop";
            optionsBuilder.UseSqlite(str);

报错Keyword not supported: 'initial catalog'.,难道是我字符串链接符配置错误了啊,谢谢

追随微笑 | 园豆:217 (菜鸟二级) | 2016-11-28 11:23

@追随微笑: ef的连接字符串和sql是不一样的.这里要的是sql的连接字符串.

在这里查http://www.connectionstrings.com/

吴瑞祥 | 园豆:28770 (高人七级) | 2016-11-28 11:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册