2012-12-19 30 views
3

我正在關注MusicStore教程我已經在Part 8 of the tutoria l上。當我試圖添加ShoppingCart類時,我得到了這個錯誤..我試圖谷歌的一個可能的解決方案,但沒有找到一個乾淨的T_T ..基於我的研究我得到這個錯誤,因爲我使用edmx這是數據庫首先代替教程中的代碼。MusicStore的'System.Data.Objects.ObjectSet <....>'沒有包含'Add'的定義,也沒有包含接受第一個參數的擴展方法'Add'

我有這個代碼加入,它正對添加錯誤()和remove()

namespace Project.Models 
{ 
    public partial class ShoppingCart 
    { 
     ProjectEntities db = new ProjectEntities(); 

     string ShoppingCartId { get; set; } 

     public const string CartSessionKey = "cart_ID"; 
     public static ShoppingCart GetCart(HttpContextBase context) 
     { 
      var cart = new ShoppingCart(); 
      cart.ShoppingCartId = cart.GetCartId(context); 
      return cart; 
     } 
     // Helper method to simplify shopping cart calls 
     public static ShoppingCart GetCart(Controller controller) 
     { 
      return GetCart(controller.HttpContext); 
     } 
     public void AddToCart(Product product) 
     { 
      // Get the matching cart and album instances 
      var cartItem = db.Carts.SingleOrDefault(c => c.cart_ID == ShoppingCartId && c.product_ID == product.product_ID); 

      if (cartItem == null) 
      { 
       // Create a new cart item if no cart item exists 
       cartItem = new Cart 
       { 
        product_ID = product.product_ID, 
        cart_ID = ShoppingCartId, 
        Count = 1, 
        DateCreated = DateTime.Now 
       }; 
       db.Carts.Add(cartItem); 
      } 
      else 
      { 
       // If the item does exist in the cart, then add one to the quantity 
       cartItem.Count++; 
      } 
      // Save changes 
      db.SaveChanges(); 
     } 
     public int RemoveFromCart(int id) 
     { 
      // Get the cart 
      var cartItem = db.Carts.Single(cart => cart.cart_ID == ShoppingCartId && cart.record_ID == id); 

      int itemCount = 0; 

      if (cartItem != null) 
      { 
       if (cartItem.Count > 1) 
       { 
        cartItem.Count--; 
        itemCount = cartItem.Count; 
       } 
       else 
       { 
        db.Carts.Remove(cartItem); 
       } 
       // Save changes 
       db.SaveChanges(); 
      } 
      return itemCount; 
     } 
     public void EmptyCart() 
     { 
      var cartItems = db.Carts.Where(cart => cart.cart_ID == ShoppingCartId); 

      foreach (var cartItem in cartItems) 
      { 
       db.Carts.Remove(cartItem); 
      } 
      // Save changes 
      db.SaveChanges(); 
     } 
     public List<Cart> GetCartItems() 
     { 
      return db.Carts.Where(cart => cart.cart_ID == ShoppingCartId).ToList(); 
     } 
     public int GetCount() 
     { 
      // Get the count of each item in the cart and sum them up 
      int? count = (from cartItems in db.Carts 
          where cartItems.cart_ID == ShoppingCartId 
          select (int?)cartItems.Count).Sum(); 
      // Return 0 if all entries are null 
      return count ?? 0; 
     } 
     public decimal GetTotal() 
     { 
      // Multiply album price by count of that album to get 
      // the current price for each of those albums in the cart 
      // sum all album price totals to get the cart total 
      decimal? total = (from cartItems in db.Carts 
           where cartItems.cart_ID == ShoppingCartId 
           select (int?)cartItems.Count * cartItems.Product.Price).Sum(); 

      return total ?? decimal.Zero; 
     } 
     public int CreateOrder(Order order) 
     { 
      decimal orderTotal = 0; 

      var cartItems = GetCartItems(); 
      // Iterate over the items in the cart, adding the order details for each 
      foreach (var item in cartItems) 
      { 
       var orderDetail = new OrderDetail 
       { 
        product_ID = item.product_ID, 
        order_ID = order.order_ID, 
        UnitPrice = item.Product.Price, 
        Quantity = item.Count 
       }; 
       // Set the order total of the shopping cart 
       orderTotal += (item.Count * item.Product.Price); 

       db.OrderDetails.Add(orderDetail); 

      } 
      // Set the order's total to the orderTotal count 
      order.Total = orderTotal; 

      // Save the order 
      db.SaveChanges(); 
      // Empty the shopping cart 
      EmptyCart(); 
      // Return the OrderId as the confirmation number 
      return order.order_ID; 
     } 
     // We're using HttpContextBase to allow access to cookies. 
     public string GetCartId(HttpContextBase context) 
     { 
      if (context.Session[CartSessionKey] == null) 
      { 
       if (!string.IsNullOrWhiteSpace(context.User.Identity.Name)) 
       { 
        context.Session[CartSessionKey] = context.User.Identity.Name; 
       } 
       else 
       { 
        // Generate a new random GUID using System.Guid class 
        Guid tempCartId = Guid.NewGuid(); 
        // Send tempCartId back to client as a cookie 
        context.Session[CartSessionKey] = tempCartId.ToString(); 
       } 
      } 
      return context.Session[CartSessionKey].ToString(); 
     } 
     // When a user has logged in, migrate their shopping cart to 
     // be associated with their username 
     public void MigrateCart(string userName) 
     { 
      var shoppingCart = db.Carts.Where(c => c.cart_ID == ShoppingCartId); 
      foreach (Cart item in shoppingCart) 
      { 
       item.cart_ID = userName; 
      } 
      db.SaveChanges(); 
     } 
    } 
} 

我在MVC首發,希望有人能幫助我解決這個問題。

+0

請把ProjectEntities'類的'代碼。 –

+0

對不起,我在數據庫中首先做了這個,所以我只有模型文件夾中的.emdx文件,它顯示了表格的關係。在我的.edmx文件是Project.Designer.cs – bot

回答

3

OK在這裏我做了什麼人使它工作..而不是使用.Add()我使用.AddObject(),而不是使用.Remove我使用.DeleteObject()..我不知道這背後是如何工作的原因,但ATLEAST現在不顯示錯誤消息..:P感謝大家誰幫我.. :)

+3

工作原因是因爲您使用Object內容聲明瞭上下文並且沒有使用DBContext。 DBContext是ObjectContext的封裝。更容易使用。不知何故,你的練習混合了2個。在地方創建模型和內容時,如果使用了正確的項目類型並且最新的nuget包含有,則應該使用使用DBContext的T4(生成代碼的模板)。您上面的代碼用於訪問從DBContext派生的上下文。建議你仔細看看Your Context def並重新生成它。強烈建議您轉移到DBcontext ;-) –

+1

whoo ..以便解釋它..謝謝..:D – bot

2

添加和刪除從EntityFrame命名空間System.Data.Entity的

所以我的猜測是使用System.Data.Entity的失蹤? 另外檢查參考項目中添加EntityFramework.dll? 或者使用包管理器(的NuGet)獲得EF項目加?, 請問您從上下文派生的DbContext?如果不是不添加。 如果你看到AddObject,你很可能從ObjectContext派生

+0

我嘗試使用System.Data.Entity的,並且還添加EntityFramework.dll在我的項目,但仍然將基準不工作.. T_T – bot

+0

嘗試重新啓動VS –

+0

我閉上我的應用程序並重新運行它,但仍然出現錯誤。T_T – bot

0

試試這個。它會消除你的錯誤,並運作良好。

而不是使用DeleteObject方法試試這個

Employee emp = new Employee(); 
foreach (int id in employeeIdsToDelete) 
{ 
    emp = db.Employees.Find(id); 
    db.Employees.Remove(emp); 
    db.SaveChanges(); 
} 
相關問題