C# - MVC application How to update and delete a record from the database
我需要知道如何更新和删除数据库中的记录。我知道如何添加记录,但无法更新和删除数据库中的记录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | namespace Ex.Models { using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class MyEntities : DbContext { public MyEntities() : base("name= MyEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public DbSet<Friend> Friend { get; set; } } } |
——
控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // POST: /Home/Edit/5 [HttpPost] public ActionResult Edit(int id, Friend f) { try { // TODO: Add update logic here myEntities.Friend.Attach(f);// Doesn't work.. How to update ? myEntities.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } } |
为了向数据库添加记录,我使用了以下代码。它奏效了;
1 2 3 | myEntities.Friend.Add(f); myEntities.SaveChanges(); return RedirectToAction("Index"); |
更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Exp.Models.Friend>" %> Delete </asp:Content> Delete Are you sure you want to delete? <fieldset> <legend>Friend</legend> Name <%: Html.DisplayFor(model => model.Name) %> </fieldset> <% using (Html.BeginForm()) { %> <p> <input type="submit" value="Delete" /> | <%: Html.ActionLink("Back to List","Index") %> </p> <% } %> </asp:Content> |
删除
1 2 | myEntities.Friend.Remove(f); myEntities.SaveChanges(); |
更新
1 2 3 | Friend f = myEntities.Friend.FirstOrDefault(x => x.Id = MyId); f.Name = NewName; myEntities.SaveChanges(); |
要更新,它与add相同,但没有
1 2 3 4 | var friendEntity = myEntites.Friend.SingleOrDefault(f => f.Id == id); friendEntity.Field1 = f.Field1; ... myEntities.SaveChanges(); |
要删除,请使用与
在索引页中(用于删除和编辑的链接)
1 2 3 4 5 6 |
编辑(第一个编辑功能是编辑页面,它获取特定ID的所有数据,第二个功能是保存更改。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public ActionResult Edit(Int32 StuID) { var studata = stu.Studs.Where(x => x.StudId == StuID).FirstOrDefault(); if (studata != null) { TempData["ID"] = StuID; TempData.Keep(); return View(studata); } return View(); } [HttpPost] public ActionResult Edit(Stud stu1) { Int32 StuID = (int)TempData["ID"]; var studata = stu.Studs.Where(x => x.StudId == StuID).FirstOrDefault(); studata.StudName = stu1.StudName; studata.StudAddress = stu1.StudAddress; studata.StudEmail = stu1.StudEmail; stu.ObjectStateManager.ChangeObjectState(studata,); stu.SaveChanges(); return RedirectToAction("Index"); } |
删除
1 2 3 4 5 6 7 8 9 10 11 12 13 | public ActionResult Delete(int StuID) { if (StuID > 0) { var studata = stu.Studs.Where(x => x.StudId == StuID).FirstOrDefault(); if (studata != null) { stu.DeleteObject(studata); stu.SaveChanges(); } } return RedirectToAction("Index"); } |
更新:第一条路
1 2 3 4 5 6 7 | public void IsActiveItem(int id) { var data = db.IRAS_InventoryItems.Find(id); data.IsActive = false; db.Entry(data).State = EntityState.Modified; db.SaveChanges(); } |
更新:第二种方法
1 2 3 4 5 6 7 8 9 | public void IsActiveItem(int id) { var data = (from a in db.IRAS_InventoryItems where a.Id == id select a).FirstOrDefault(); data.IsActive = false; db.Entry(data).State = EntityState.Modified; db.SaveChanges(); } |
移除:首先
1 2 3 4 5 6 7 | public void Remove(int id) { var data = db.IRAS_InventoryItems.Find(id); data.IsActive = false; db.IRAS_InventoryItems.Remove(data); db.SaveChanges(); } |
删除:秒
1 2 3 4 5 6 7 8 | public void Remove(int id) { var data = (from a in db.IRAS_InventoryItems where a.Id == id select a).FirstOrDefault(); db.IRAS_InventoryItems.Remove(data); db.SaveChanges(); } |
只需proove的concept controller.updateModel不能有效地工作。
全班在这里https://stackoverflow.com/a/39452785/1071165
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | const string PK ="Id"; protected Models.Entities con; protected System.Data.Entity.DbSet<T> model; [HttpPost] public virtual ActionResult AddEdit(T item) { TestUpdate(item); con.SaveChanges(); return RedirectToAction("Index"); } [HttpGet] public virtual ActionResult Remove(string id) { int nId = 0; int.TryParse(id, out nId); if (nId != 0) { var item = model.Find(nId); con.Entry(item).State = System.Data.Entity.EntityState.Deleted; con.SaveChanges(); } return Redirect(Request.UrlReferrer.ToString()); } private void TestUpdate(object item) { var props = item.GetType().GetProperties(); foreach (var prop in props) { object value = prop.GetValue(item); if (prop.PropertyType.IsInterface && value != null) { foreach (var iItem in (System.Collections.IEnumerable)value) { TestUpdate(iItem); } } } int id = (int)item.GetType().GetProperty(PK).GetValue(item); if (id == 0) { con.Entry(item).State = System.Data.Entity.EntityState.Added; } else { con.Entry(item).State = System.Data.Entity.EntityState.Modified; } } |
更新:
1 2 3 4 5 6 7 8 9 10 11 12 | if (ModelState.IsValid && f != null) { myEntities.Friend.Attach(f); var upd = (from c in myEntities.Friend where c.Id == f.Id select c).FirstOrDefault(); upd.Data1=f.Data1; ... .... myEntities.ObjectStateManager.ChangeObjectState(f, EntityState.Modified); myEntities.SaveChanges(); } |
删除:
1 2 3 4 5 6 7 8 9 | if (ModelState.IsValid && f != null) { var del = (from c in myEntities.Friend where c.Id == f.Id select c).FirstOrDefault(); myEntities.Friend.DeleteObject(del); myEntities.SaveChanges(); } |