Excel 文件上传失败,因为 'db' 在当前上下文中不存在

Excel file upload failed due 'db' does not exist in the current context

我正在尝试上传 excel 文件,然后在 database.I 上显示并保存它 我正在使用 Entity Framework 代码 first.If 我显示文件,但它工作正常但是如果我尝试将它保存到我当前的数据上下文中,我会收到此 错误“'db' 在当前上下文中不存在” 任何建议都会很好地提示 me.While 显示我想保存相同 excel file.If 我的方法不正确,请告诉我如何将数据表保存到基于 class 的对象中。 我有一个 class 文件

public class dataextract
 {

    public string Code { get; set; }
    public string Name1 { get; set; }
    public string Group1 { get; set; }

}

在数据上下文中

public class ReadContext : DbContext
{
    public ReadContext()
       : base("name = ExcelConnection")
    {
        Database.SetInitializer(new 
          MigrateDatabaseToLatestVersion<ReadContext,      
                 ReadAndDisplayExcel.Migrations.Configuration>());
    }
    public DbSet<dataextract> dataext { get; set; }

}

在控制器中

   public ActionResult Index(HttpPostedFileBase uploadfile)
    {
        //List<dataextract> lstStudent = new List<dataextract>();
        if (ModelState.IsValid)
        {
            if (uploadfile != null && uploadfile.ContentLength > 0)
            {
                //ExcelDataReader works on binary excel file
                Stream stream = uploadfile.InputStream;
                //We need to written the Interface.
                IExcelDataReader reader = null;
                if (uploadfile.FileName.EndsWith(".xls"))
                {
                    //reads the excel file with .xls extension
                    reader = ExcelReaderFactory.CreateBinaryReader(stream);
                }
                else if (uploadfile.FileName.EndsWith(".xlsx"))
                {
                    //reads excel file with .xlsx extension
                    reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                }
                else
                {
                    //Shows error if uploaded file is not Excel file
                    ModelState.AddModelError("File", "This file format is not supported");
                    return View();
                }               


                var conf = new ExcelDataSetConfiguration
                {
                    ConfigureDataTable = _ => new ExcelDataTableConfiguration
                    {
                        UseHeaderRow = true
                    }
                };

                DataSet result = reader.AsDataSet(conf);
                DataTable s1 = result.Tables[0];

                reader.Close();                  
                var query = from s in s1.AsEnumerable()

                             select new
                             {

                                 Code = s.Field<string>("Code"),
                                 Name1 = s.Field<string>("Name1"),
                                 Group1 = s.Field<string>("Group1")
                             };

                 using (ReadContext db = new ReadContext())
                 {
                     foreach (var ss in query)//error "'db' does not exist in the current context"
                     {
                         db.dataext.Add(ss);
                     }
                     db.SaveChanges();

                 }

                return View(s1);
            }
        }
        else
        {
            ModelState.AddModelError("File","Please upload your file");
        }
        return View();
  } 
using (ReadContext db = new ReadContext())
            {
                   try
                    {
                        var query = from s in s1.AsEnumerable()

                                    select new
                                    {


                                        CODE = s.Field<string>("CODE").ToString(),
                                        Name = s.Field<string>("Name").ToString(),
                                        Group = s.Field<string>("Group").ToString()
                                    };
                        var q1 = query.Select(x => new dataextract
                        {


                            CODE = x.CODE,
                            Name = x.CompanyName,
                            Group = x.Group,
                        }).ToList();

                        foreach (var ss in q1)
                        {

                            db.dataext.Add(ss);
                        }
                        db.SaveChanges();
                    }
            }