1

我的創建和刪除操作成功處理成功,沒有任何錯誤,但什麼都不做。當我嘗試創建新產品並保存時,它不會顯示在我的桌子上。當我嘗試刪除產品時,它不會被刪除。NET C#CRUD操作工作,但永遠不會在數據庫中得到更新

我相信這個問題是在我的InventoryContext.cs在這條線的文件:

public class InventoryContext : System.Data.Entity.DbContext 

我不得不添加System.Data.Entity.DbContext爲db.SaveChanges()不顯示錯誤。

當我編輯一些產品並保存它,它給了我這樣的:

enter image description here

ProductController.cs

using System; 
    using System.Collections.Generic; 
    using System.Data; 
    using System.Data.Entity; 
    using System.Linq; 
    using System.Net; 
    using System.Web; 
    using System.Web.Mvc; 
    using Inventory.DAL; 
    using Inventory.Models; 

    namespace Inventory.Controllers 
    { 
     public class ProductController : Controller 
     { 
      private InventoryContext db = new InventoryContext(); 

      // GET: Product 
     public ActionResult Index() 
     { 
      return View(db.Products.ToList()); 
     } 

     public ActionResult Details(int id) 
     { 
      Product product = db.Products.FirstOrDefault(c => c.Id == id); 

      // there error handling should be better than this 
      if (product == null) 
       return HttpNotFound(); 

      return View(product); 
     } 

     // GET: Product/Edit/5 
     public ActionResult Edit(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Product product = db.Products.FirstOrDefault(c => c.Id == id); 
      if (product == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(product); 
     } 

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit([Bind(Include = "Id,CategoryId,Brand,Name,Barcode,Price")] Product product) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(product).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(product); 
     } 

     /// GET: Product/Create 
     public ActionResult Create() 
     { 
      return View(); 
     } 


     [HttpPost, ActionName("Create")] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create([Bind(Include = "Id,CategoryId,Brand,Name,Barcode,Price")] Product product) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Products.Add(product); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(product); 
     } 


     public ActionResult Delete(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Product product = db.Products.FirstOrDefault(c => c.Id == id); 
      if (product == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(product); 
     } 
     [HttpPost, ActionName("Delete")] 
     [ValidateAntiForgeryToken] 
     public ActionResult DeleteConfirmed(int id) 
     { 
      Product product = db.Products.FirstOrDefault(c => c.Id == id); 
      db.Products.Remove(product); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing) 
      { 
       db.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 


     public ActionResult Assign(int productId, int categoryId) 
     { 
      Product product = db.Products.FirstOrDefault(p => p.Id == productId); 
      product.CategoryId = categoryId; 
      return RedirectToAction("Index"); 
     } 


    } 
} 

InventoryContext.cs

using System.Collections.Generic; 
using Inventory.Models; 
using System.Linq; 

namespace Inventory.DAL 
{ 
    public class InventoryContext : System.Data.Entity.DbContext 
    { 
     public List<Product> Products { get; set; } 
     public List<Category> Categories { get; set; } 

     public InventoryContext() 
     { 
      Products = new List<Product> 
      { 
      new Product{Id=1,CategoryId=1,Brand="Coca Cola", Name="Coca Cola",Barcode="00001",Price=150}, 
      new Product{Id=2,CategoryId=1,Brand="Pepsi", Name="Pepsi",Barcode="00011",Price=150}, 
      new Product{Id=3,CategoryId=2,Brand="Homebrand", Name="Baked Beans",Barcode="0002",Price=250}, 
      new Product{Id=4,CategoryId=2,Brand="Homebrand", Name="Baked Patatos",Barcode="0022",Price=250} 

      }; 
      Categories = new List<Category> 
      { 
      new Category{ID=1, Name="Drink", Products = Products.Where(p => p.CategoryId == 1).ToList() }, 
      new Category{ID=2,Name="Canned Food", Products = Products.Where(p => p.CategoryId == 2).ToList() } 

      }; 
      foreach (var product in Products) 
       product.Categories = Categories.Where(c => c.ID == product.CategoryId).ToList(); 

     } 
    } 
} 

回答

0

在InventoryContext .cs文件請通過添加下面的行來檢查。

公共System.Data.Entity.DbSet產品{獲得;設置;} 公共System.Data.Entity.DbSet分類{獲取;集;}

漱口分辨率下面的鏈接。 http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-a-model

+2

你的意思是public System.Data.Entity.DbSet Product {get;組; } ?? 如果我使用公共System.Data.Entity.DbSet產品{get;組; } 它與公開名單衝突產品{get;組; } – TykiMikk

+2

System.Data.Entity.DbSet Product {get;組; } System.Data.Entity.DbSet 類別{get;組; } ?? 給出了以下錯誤:Invetory.DAL.InventoryContext.Products是'屬性',但用作'type' – TykiMikk

+2

是的,該類被命名爲Product。我試圖創造一些東西,但從未更新。 – TykiMikk

相關問題