C# MusicStore';System.Data.Objects.ObjectSet<&燃气轮机';不包含';添加';没有扩展方法';添加';接受第一个论点
我正在学习MusicStore教程,我已经在l。我在尝试添加ShoppingCart类时遇到此错误。。我试图用谷歌搜索一个可能的解决方案,但没能找到一个干净的。。根据我的研究,我得到了这个错误,因为我使用的是edmx,它是数据库优先,而不是教程中的代码优先 我添加了这些代码,但在Add()和Remove()上出现错误C# MusicStore';System.Data.Objects.ObjectSet<&燃气轮机';不包含';添加';没有扩展方法';添加';接受第一个论点,c#,asp.net,asp.net-mvc-3,entity-framework,objectset,C#,Asp.net,Asp.net Mvc 3,Entity Framework,Objectset,我正在学习MusicStore教程,我已经在l。我在尝试添加ShoppingCart类时遇到此错误。。我试图用谷歌搜索一个可能的解决方案,但没能找到一个干净的。。根据我的研究,我得到了这个错误,因为我使用的是edmx,它是数据库优先,而不是教程中的代码优先 我添加了这些代码,但在Add()和Remove()上出现错误 namespace项目.模型 { 公共部分类购物车 { ProjectEntities db=新的ProjectEntities(); 字符串ShoppingCartId{g
namespace项目.模型
{
公共部分类购物车
{
ProjectEntities db=新的ProjectEntities();
字符串ShoppingCartId{get;set;}
public const string CartSessionKey=“cart\u ID”;
公共静态ShoppingCart GetCart(HttpContextBase上下文)
{
var cart=new ShoppingCart();
cart.ShoppingCartId=cart.GetCartId(上下文);
返回车;
}
//简化购物车调用的Helper方法
公共静态ShoppingCart GetCart(控制器)
{
返回GetCart(controller.HttpContext);
}
公共无效AddToCart(产品)
{
//获取匹配的购物车和相册实例
var cartim=db.Carts.SingleOrDefault(c=>c.cart\u ID==ShoppingCartId&&c.product\u ID==product.product\u ID);
如果(cartItem==null)
{
//如果不存在购物车项目,则创建新购物车项目
cartItem=新购物车
{
product\U ID=product.product\U ID,
购物车ID=购物车ID,
计数=1,
DateCreated=DateTime.Now
};
db.Carts.Add(cartItem);
}
其他的
{
//如果商品确实存在于购物车中,则在数量中添加一个
cartItem.Count++;
}
//保存更改
db.SaveChanges();
}
公共int RemoveFromCart(int id)
{
//上车
var cartim=db.Carts.Single(cart=>cart.cart\u ID==ShoppingCartId&&cart.record\u ID==ID);
int itemCount=0;
如果(cartItem!=null)
{
如果(cartItem.Count>1)
{
cartItem.Count--;
itemCount=cartItem.Count;
}
其他的
{
db.Carts.Remove(cartItem);
}
//保存更改
db.SaveChanges();
}
返回项目计数;
}
公共空间清空部分()
{
var cartItems=db.Carts.Where(cart=>cart.cart\u ID==ShoppingCartId);
foreach(cartItems中的var cartItem)
{
db.Carts.Remove(cartItem);
}
//保存更改
db.SaveChanges();
}
公共列表GetCartItems()
{
返回db.Carts.Where(cart=>cart.cart_ID==ShoppingCartId.ToList();
}
public int GetCount()
{
//获取购物车中每件商品的数量并将其汇总
int?count=(来自db.Carts中的cartItems
其中cartItems.cart_ID==ShoppingCartId
选择(int?)cartItems.Count.Sum();
//如果所有条目都为空,则返回0
返回计数??0;
}
公共十进制GetTotal()
{
//将唱片价格乘以该唱片的计数即可得到
//购物车中每个相册的当前价格
//将所有专辑价格总计相加,以获得购物车总计
decimal?total=(来自数据库Carts中的cartItems)
其中cartItems.cart_ID==ShoppingCartId
选择(int?)cartItems.Count*cartItems.Product.Price.Sum();
返回总计??小数点零;
}
公共int CreateOrder(订单)
{
小数顺序合计=0;
var cartItems=GetCartItems();
//迭代购物车中的项目,为每个项目添加订单详细信息
foreach(cartItems中的var项)
{
var orderDetail=新的orderDetail
{
product\u ID=item.product\u ID,
order\u ID=order.order\u ID,
单价=item.Product.Price,
数量=项目。计数
};
//设置购物车的订单总数
orderTotal+=(item.Count*item.Product.Price);
db.OrderDetails.Add(orderDetail);
}
//将订单总数设置为orderTotal计数
订单总数=订单总数;
//保存订单
db.SaveChanges();
//清空购物车
EmptyCart();
//返回OrderId作为确认号
退货订单。订单号;
}
//我们正在使用HttpContextBase来允许访问Cookie。
公共字符串GetCartId(HttpContextBase上下文)
{
if(context.Session[CartSessionKey]==null)
{
if(!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
context.Session[CartSessionKey]=context.User.Identity.Name;
}
其他的
{
//使用System.GUID类生成新的随机GUID
Guid tempCartId=Guid.NewGuid();
//将tempCartId作为cookie发送回客户端
context.Session[CartSessionKey]=tempCartId.ToString();
}
}
返回context.Session[CartSessionKey
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();
}
}
}
Employee emp = new Employee();
foreach (int id in employeeIdsToDelete)
{
emp = db.Employees.Find(id);
db.Employees.Remove(emp);
db.SaveChanges();
}
public JsonResult getEmployeeByNo(string EmpNo)
{
using (SampleDBangularEntities dataContext = new SampleDBangularEntities())
{
int no = Convert.ToInt32(EmpNo);
var employeeList = dataContext.Employees.Find(no);
return Json(employeeList, JsonRequestBehavior.AllowGet);
}
}