title: Unable to set field/property CartItems on entity type BusinessEntitys.Product
body: <p>I spent many hours (including wide Google search) trying to figure out what the problem is. Unfortunately, without success.
  The error that I get is: "Unable to set field/property CartItems on entity type BusinessEntitys.Product. See InnerException for details."
  the <strong>innerExeption</strong> is <strong>null</strong>.
  The error occurs at addToCart() function when I try to call a function <b>addCartItem</b> through the WCF. When the call get to DAL and try to save the new object in db context it's failed.</p>
  
  <pre><code>public void AddToCart(int id)
  {
      // Retrieve the product from the database.           
      ShoppingCartId = GetCartId();
  
      CartItem cartItem = new BLFrontend().getAllCartItems().SingleOrDefault(
           c =&gt; c.CartItemId == ShoppingCartId
           &amp;&amp; c.ProductId == id);
      if (cartItem == null)
      {
          Product temp = new BLFrontend().getAllProducts().SingleOrDefault(
               p =&gt; p.ProductId == id);
          // Create a new cart item if no cart item exists.                 
          cartItem = new CartItem
          {
              CartItemId = Guid.NewGuid().ToString(),
              ProductId = id,
              CartId_ = ShoppingCartId,
              Product = temp,
              Quantity = 1,
          };
  
          new BLFrontend().addCartItem(cartItem);//The problem starts from here
      }
      else
      {
          cartItem.Quantity++;
          //update
      }
  </code></pre>
  
  <p>I'm using EF in my project, this is the Model:</p>
  
  <pre><code>namespace BusinessEntitys
  {
  using System;
  using System.Collections.Generic;
  
  public partial class CartItem
  {
      public string CartItemId { get; set; }
      public string CartId_ { get; set; }
      public int Quantity { get; set; }
      public int ProductId { get; set; }
  
      public virtual Product Product { get; set; }
  }
  }
  </code></pre>
  
  <p>class Product that contained in CratItem:</p>
  
  <pre><code>    namespace BusinessEntitys
      {
      using System;
      using System.Collections.Generic;
      using System.Linq; // added this
  
      public partial class Product
      {
          public Product()
          {
              this.ItemInOrders = new HashSet&lt;ItemInOrder&gt;();
              this.CartItems = new HashSet&lt;CartItem&gt;();
          }
  
          public int ProductId { get; set; }
          public string ProductName { get; set; }
          public double ProductBasePrice { get; set; }
          public double ProductDiscount { get; set; }
          public string ProductDescription { get; set; }
          public string ProductImageURL { get; set; }
          public string ProductQualityLevel { get; set; }
          public string ProductCategory { get; set; }
  
          public virtual ICollection&lt;CartItem&gt; CartItems { get; set; }
      }
  }
  </code></pre>
  
  <p>My BusinessEntitys is:</p>
  
  <pre><code>namespace BusinessEntitys
  {
      using System;
      using System.Data.Entity;
      using System.Data.Entity.Infrastructure;
  
      public partial class StoreDBEntities : DbContext
      {
          public StoreDBEntities()
              : base("name=StoreDBEntities")
          {
              base.Configuration.ProxyCreationEnabled = false;
          }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              throw new UnintentionalCodeFirstException();
          }
  
          public virtual DbSet&lt;Client&gt; Clients { get; set; }
          public virtual DbSet&lt;ItemInOrder&gt; ItemInOrders { get; set; }
          public virtual DbSet&lt;Order&gt; Orders { get; set; }
          public virtual DbSet&lt;Product&gt; Products { get; set; }
          public virtual DbSet&lt;StockManagment&gt; StockManagments { get; set; }
          public virtual DbSet&lt;CartItem&gt; CartItems { get; set; }
      }
  }
  </code></pre>
  
  <p>Execption occurs here in my DAL at 2nd line when trying to execute the command:
  <strong>context.CartItems.Add(CartItemToAdd)</strong></p>
  
  <pre><code> public bool addCartItem(CartItem CartItemToAdd)
          {
              try
              {
                  var context = new StoreDBEntities();
                  context.CartItems.Add(CartItemToAdd);
                  return context.SaveChanges() &gt; 0;
  
              }
              catch (Exception ex)
              {
                  throw new Exception(ex.Message);
              }
          }
  </code></pre>
  
  <p>The stackTracer:</p>
  
  <blockquote>
    <p>StackTrace    "at DataAccess.Dal.addCartItem(CartItem CartItemToAdd) in
    c:\Users\User\Documents\Visual Studio
    2013\Projects\OrGarden\DataAccess\Dal.cs:line 406\r\n   at
    BusinessLogicBackend.BLBackend.addCartItem(CartItem cartItemToAdd) in
    c:\Users\User\Documents\Visual Studio
    2013\Projects\OrGarden\BusinessLogicBackend\BLBackend.cs:line
    293\r\n   at SyncInvokeaddCartItem(Object , Object[] , Object[] )\r\n 
    at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object
    instance, Object[] inputs, Object[]&amp; outputs)\r\n   at
    System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp;
    rpc)" string</p>
  </blockquote>
  
  <p>Important Update: When i deleted this assignment <code>Product = temp,</code> the <strong>addCartItem</strong> call succeed saved without a problem. Only that the field <strong>Product</strong> remains <strong>null</strong>.
  The code after that change:</p>
  
  <pre><code>Product temp =  business.getAllProducts().SingleOrDefault(
                       p =&gt; p.ProductId == id);
                  // Create a new cart item if no cart item exists.                 
                  cartItem = new CartItem
                  {
                      CartItemId = Guid.NewGuid().ToString(),
                      ProductId = id,
                      CartId_ = ShoppingCartId,
                      Product = temp,
                      Quantity = 1,
                  };
  
                   business.addCartItem(cartItem);
  </code></pre>
  
  <p>Whene i retrieved CrartItem from the context the field <strong>Product</strong> remains <strong>null</strong>. </p>
  
  <p>Can anyone help me fix that bad problem??</p>
  
  <p>Thank you very much,
  Ron</p>