mysql 不能用dbcontext_c#-“因为DbContext已被处置,所以操作无法完...

我正在使用EF和Oracle数据库构建一个简单的ASP.NET API.当我想从数据库表中获取所有元素时,响应(500)表示“由于DbContext已被处理,因此无法完成该操作”.

?

好吧,在将其发布到这里之前,我已经尝试解决此问题.但是我不能.我的控制器代码如下.

public class PruebasController : ApiController

{

//Prueba[] pruebas = new Prueba[]

//{

// new Prueba { Name = "Tomato Soup"},

// new Prueba { Name = "Yo-yo"},

// new Prueba { Name = "Hammer"}

//};

public IQueryable GetAllPruebas()

{

Database.SetInitializer(new DropCreateDatabaseAlways());

using (var ctx = new OracleDbContext())

{

return ctx.Pruebas;

}

}

}

(如您所见,我有一个“ pruebas”列表,当我返回它时,http服务有效)

这是我的OracleDbContext

public class OracleDbContext : DbContext

{

public DbSet Pruebas { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

modelBuilder.HasDefaultSchema("DATA");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个系统可以包括以下功能: 1. 学生管理:添加、删除、修改、查询学生信息; 2. 课程管理:添加、删除、修改、查询课程信息; 3. 成绩管理:录入、修改、查询学生成绩; 4. 用户登录:学生、教师、管理员登录; 5. 数据库管理:备份、还原数据库。 首先,我们需要创建一个 ASP.NET MVC 4 项目,并在项目中创建以下模型类: ```c# public class Student { public int Id { get; set; } public string Name { get; set; } public string Gender { get; set; } public string Major { get; set; } } public class Course { public int Id { get; set; } public string Name { get; set; } } public class Score { public int Id { get; set; } public int StudentId { get; set; } public Student Student { get; set; } public int CourseId { get; set; } public Course Course { get; set; } public double Grade { get; set; } } ``` 接下来,我们需要创建一个数据库上下文类,并在其中定义上述模型类对应的 DbSet: ```c# public class ScoreContext : DbContext { public DbSet<Student> Students { get; set; } public DbSet<Course> Courses { get; set; } public DbSet<Score> Scores { get; set; } } ``` 然后,我们需要创建一个控制器类,用于处理学生、课程、成绩的增删改查操作: ```c# public class HomeController : Controller { private ScoreContext db = new ScoreContext(); // 学生管理 public ActionResult Students() { var students = db.Students.ToList(); return View(students); } public ActionResult AddStudent() { return View(); } [HttpPost] public ActionResult AddStudent(Student student) { db.Students.Add(student); db.SaveChanges(); return RedirectToAction("Students"); } public ActionResult EditStudent(int id) { var student = db.Students.Find(id); return View(student); } [HttpPost] public ActionResult EditStudent(Student student) { db.Entry(student).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Students"); } public ActionResult DeleteStudent(int id) { var student = db.Students.Find(id); db.Students.Remove(student); db.SaveChanges(); return RedirectToAction("Students"); } // 课程管理 public ActionResult Courses() { var courses = db.Courses.ToList(); return View(courses); } public ActionResult AddCourse() { return View(); } [HttpPost] public ActionResult AddCourse(Course course) { db.Courses.Add(course); db.SaveChanges(); return RedirectToAction("Courses"); } public ActionResult EditCourse(int id) { var course = db.Courses.Find(id); return View(course); } [HttpPost] public ActionResult EditCourse(Course course) { db.Entry(course).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Courses"); } public ActionResult DeleteCourse(int id) { var course = db.Courses.Find(id); db.Courses.Remove(course); db.SaveChanges(); return RedirectToAction("Courses"); } // 成绩管理 public ActionResult Scores() { var scores = db.Scores.Include(s => s.Student).Include(s => s.Course).ToList(); return View(scores); } public ActionResult AddScore() { ViewBag.StudentId = new SelectList(db.Students, "Id", "Name"); ViewBag.CourseId = new SelectList(db.Courses, "Id", "Name"); return View(); } [HttpPost] public ActionResult AddScore(Score score) { db.Scores.Add(score); db.SaveChanges(); return RedirectToAction("Scores"); } public ActionResult EditScore(int id) { var score = db.Scores.Find(id); ViewBag.StudentId = new SelectList(db.Students, "Id", "Name", score.StudentId); ViewBag.CourseId = new SelectList(db.Courses, "Id", "Name", score.CourseId); return View(score); } [HttpPost] public ActionResult EditScore(Score score) { db.Entry(score).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Scores"); } public ActionResult DeleteScore(int id) { var score = db.Scores.Find(id); db.Scores.Remove(score); db.SaveChanges(); return RedirectToAction("Scores"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } ``` 最后,我们需要创建视图文件,用于展示学生、课程、成绩的信息以及进行增删改查操作。 学生管理视图: ```html @model List<Student> <h2>学生管理</h2> <p> <a href="@Url.Action("AddStudent")">添加学生</a> </p> <table> <tr> <th>ID</th> <th>姓名</th> <th>性别</th> <th>专业</th> <th>操作</th> </tr> @foreach (var student in Model) { <tr> <td>@student.Id</td> <td>@student.Name</td> <td>@student.Gender</td> <td>@student.Major</td> <td> <a href="@Url.Action("EditStudent", new { id = student.Id })">编辑</a> <a href="@Url.Action("DeleteStudent", new { id = student.Id })">删除</a> </td> </tr> } </table> ``` 添加学生视图: ```html @model Student <h2>添加学生</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div> <label for="Name">姓名:</label> @Html.TextBoxFor(model => model.Name) </div> <div> <label for="Gender">性别:</label> @Html.TextBoxFor(model => model.Gender) </div> <div> <label for="Major">专业:</label> @Html.TextBoxFor(model => model.Major) </div> <p> <input type="submit" value="提交" /> </p> } ``` 课程管理视图和添加课程视图类似,这里不再赘述。成绩管理视图: ```html @model List<Score> <h2>成绩管理</h2> <p> <a href="@Url.Action("AddScore")">添加成绩</a> </p> <table> <tr> <th>ID</th> <th>学生</th> <th>课程</th> <th>成绩</th> <th>操作</th> </tr> @foreach (var score in Model) { <tr> <td>@score.Id</td> <td>@score.Student.Name</td> <td>@score.Course.Name</td> <td>@score.Grade</td> <td> <a href="@Url.Action("EditScore", new { id = score.Id })">编辑</a> <a href="@Url.Action("DeleteScore", new { id = score.Id })">删除</a> </td> </tr> } </table> ``` 添加成绩视图: ```html @model Score <h2>添加成绩</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div> <label for="StudentId">学生:</label> @Html.DropDownList("StudentId", null, "-- 请选择 --") </div> <div> <label for="CourseId">课程:</label> @Html.DropDownList("CourseId", null, "-- 请选择 --") </div> <div> <label for="Grade">成绩:</label> @Html.TextBoxFor(model => model.Grade) </div> <p> <input type="submit" value="提交" /> </p> } ``` 以上就是一个简单的学生考试成绩管理系统的实现,可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值