实体类型CartItem不是当前上下文的模型的一部分

时间:2014-07-16 18:28:23

标签: c# asp.net entity-framework dbcontext

我正在关注Wingtip Toys教程。我创建了数据库,然后从数据库生成了Entitymodel。当我运行项目将项目添加到cartitem我得到"实体类型CartItem不是当前上下文模型的一部分。"我将不胜感激您的任何建议。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WingtipToys.Models;

namespace WingtipToys.Logic
{
    public class ShoppingCartActions : IDisposable
{
    public string ShoppingCartId { get; set; }

    private wingtiptoysEntities _db = new wingtiptoysEntities();


    public const string CartSessionKey = "CartId";

    public void AddToCart(int id)
    {
        // Retrieve the product from the database.           
        ShoppingCartId = GetCartId();

        **var cartItem = _db.CartItems.SingleOrDefault(
            c => c.CartId == ShoppingCartId
            && c.ProductId == id);**
        if (cartItem == null)
        {
            // Create a new cart item if no cart item exists.                 
            cartItem = new CartItem
            {
                ItemId = Guid.NewGuid().ToString(),
                ProductId = id,
                CartId = ShoppingCartId,
                Product = _db.Products.SingleOrDefault(
                 p => p.ProductID == id),
                Quantity = 1,
                DateCreated = DateTime.Now
            };

            _db.CartItems.Add(cartItem);
        }
        else
        {
            // If the item does exist in the cart,                  
            // then add one to the quantity.                 
            cartItem.Quantity++;
        }
        _db.SaveChanges();
    }

    public void Dispose()
    {
        if (_db != null)
        {
            _db.Dispose();
            _db = null;
        }
    }

    public string GetCartId()
    {
        if (HttpContext.Current.Session[CartSessionKey] == null)
        {
            if (!string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name))
            {


HttpContext.Current.Session[CartSessionKey] =HttpContext.Current.User.Identity.Name;
        }
            else
            {
                // Generate a new random GUID using System.Guid class.     
                Guid tempCartId = Guid.NewGuid();
            HttpContext.Current.Session[CartSessionKey] = tempCartId.ToString();
        }
        }
        return HttpContext.Current.Session[CartSessionKey].ToString();
    }

    public List<CartItem> GetCartItems()
    {
        ShoppingCartId = GetCartId();

        return _db.CartItems.Where(
            c => c.CartId == ShoppingCartId).ToList();
    }
}

}

Heare是cartItem的Cs文件

namespace WingtipToys.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;


    public partial class CartItem
    {
        [Key]
        public string ItemId { get; set; }
        public string CartId { get; set; }
        public Nullable<int> Quantity { get; set; }
        public Nullable<System.DateTime> DateCreated { get; set; }
        public Nullable<int> ProductId { get; set; }
        public virtual Product Product { get; set; }


    }
}

这是Context.cs

namespace WingtipToys.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class wingtiptoysEntities : DbContext
    {
        public wingtiptoysEntities()
            : base("name=wingtiptoysEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<CartItem> CartItems { get; set; }
        public virtual DbSet<Category> Categories { get; set; }
        public virtual DbSet<Product> Products { get; set; }
    }
}

当我想从数据库中检索产品时出现错误。下面是我得到错误的部分

    ShoppingCartId = GetCartId();

   var cartItem = _db.CartItems.SingleOrDefault(
        c => c.CartId == ShoppingCartId
        && c.ProductId == id);

0 个答案:

没有答案