2013-01-09 59 views
1

我使用音乐商店购物车代码为我的mvc项目,但我的项目使用LINQ到SQL,而不是实体框架。我已经改变了MusicStoreEntities类如下:Mvc音乐商店LINQ到SQL购物车

public class ShoppingCartEntities 
{ 
    public ShoppingCartEntities() 
    { 
    } 
    public List<h2t_Store_Product> Products { get; set; } 
    public List<Cart> Carts { get; set; } 
    public List<Order> Orders { get; set; } 
    public List<OrderDetail> OrderDetails { get; set; } 
} 

而这些是ShoppingCart.cs类:

public class ShoppingCart 
{ 
    ShoppingCartEntities storeDB = new ShoppingCartEntities(); 
    string ShoppingCartId { get; set; } 
    public const string CartSessionKey = "CartId"; 
    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(h2t_Store_Product product) 
    { 
     // Get the matching cart and album instances 
     var cartItem = (from data in storeDB.Carts 
         where data.CartId == ShoppingCartId && data.ProductID == product.ID 
         select data).Single(); 

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

     int itemCount = 0; 

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

     foreach (var cartItem in cartItems) 
     { 
      storeDB.Carts.Remove(cartItem); 
     } 
     // Save changes 
     //storeDB.SaveChanges(); 
    } 
    public List<Cart> GetCartItems() 
    { 
     return storeDB.Carts.Where(
      cart => cart.CartId == ShoppingCartId).ToList(); 
    } 
    public int GetCount() 
    { 
     // Get the count of each item in the cart and sum them up 
     int? count = (from cartItems in storeDB.Carts 
         where cartItems.CartId == 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 storeDB.Carts 
          where cartItems.CartId == ShoppingCartId 
          select (int?)cartItems.Count * 
          cartItems.product.UnitPrice).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 
      { 
       AlbumId = item.ProductID, 
       OrderId = order.OrderId, 
       UnitPrice = item.product.UnitPrice, 
       Quantity = item.Count 
      }; 
      // Set the order total of the shopping cart 
      orderTotal += (item.Count * item.product.UnitPrice); 

      storeDB.OrderDetails.Add(orderDetail); 

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

     // Save the order 
     //storeDB.SaveChanges(); 
     // Empty the shopping cart 
     EmptyCart(); 
     // Return the OrderId as the confirmation number 
     return order.OrderId; 
    } 
    // 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 = storeDB.Carts.Where(
      c => c.CartId == ShoppingCartId); 

     foreach (Cart item in shoppingCart) 
     { 
      item.CartId = userName; 
     } 
     //storeDB.SaveChanges(); 
    } 
} 

映Cart.cs类:

public class Cart 
{ 
    [Key] 
    public int RecordId { get; set; } 
    public string CartId { get; set; } 
    public int ProductID { get; set; } 
    public int Count { get; set; } 
    public System.DateTime DateCreated { get; set; } 
    public virtual h2t_Store_Product product { get; set; } 

} 

当我运行的项目,我得到一个错误:

值不能为空。参数名:源

enter image description here

任何人都可以告诉我怎么解决?非常感谢你。

回答

0

这不会作为linq-to-sql运行,而是作为linq-to-objects运行。 linq查询的来源是ShoppingCartEntities.Carts,这是一个List<Cart>

异常消息表示执行查询时Cartsnull。我看不到你在提供的代码中的任何地方检查了这个列表。

如果您试图将其从EF Code First示例中转换出来,很容易错过该步骤。当使用EF Code First时,实体集会自动从基类中获取值(可通过反射推测),但linq-to-sql中不存在“magic”。

+0

嗨安德斯,就像你说的。我试过从“EF Code First”转换为“LINQ to SQL”,这是不可能的? – thanh

+0

这肯定是可能的,但它看起来像你不熟悉LINQ to SQL,所以你应该在尝试转换之前在LINQ to SQL上做一些教程,以便知道适当的LINQ to SQL代码的样子。 –

+0

是的,没错。我不熟悉LINQ to SQL,我会了解更多。顺便说一句,你有一个简单的mvc asp.net购物车代码?我想学习在mvc上建立一个购物车,但是我摸索起来太困难了。谢谢你的回答:) – thanh