【发布时间】:2015-05-27 22:02:07
【问题描述】:
我花了很多时间(包括广泛的 Google 搜索)试图找出问题所在。不幸的是,没有成功。 我得到的错误是:“无法在实体类型 BusinessEntitys.Product 上设置字段/属性 CartItems。有关详细信息,请参阅 InnerException。” innerExeption 为 null。 当我尝试通过 WCF 调用函数 addCartItem 时,addToCart() 函数发生错误。当调用到达 DAL 并尝试将新对象保存在 db 上下文中时,它失败了。
public void AddToCart(int id)
{
// Retrieve the product from the database.
ShoppingCartId = GetCartId();
CartItem cartItem = new BLFrontend().getAllCartItems().SingleOrDefault(
c => c.CartItemId == ShoppingCartId
&& c.ProductId == id);
if (cartItem == null)
{
Product temp = new BLFrontend().getAllProducts().SingleOrDefault(
p => 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
}
我在我的项目中使用 EF,这是模型:
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; }
}
}
CratItem 中包含的类产品:
namespace BusinessEntitys
{
using System;
using System.Collections.Generic;
using System.Linq; // added this
public partial class Product
{
public Product()
{
this.ItemInOrders = new HashSet<ItemInOrder>();
this.CartItems = new HashSet<CartItem>();
}
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<CartItem> CartItems { get; set; }
}
}
我的业务实体是:
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<Client> Clients { get; set; }
public virtual DbSet<ItemInOrder> ItemInOrders { get; set; }
public virtual DbSet<Order> Orders { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<StockManagment> StockManagments { get; set; }
public virtual DbSet<CartItem> CartItems { get; set; }
}
}
尝试执行命令时,在我的 DAL 中的第 2 行执行: context.CartItems.Add(CartItemToAdd)
public bool addCartItem(CartItem CartItemToAdd)
{
try
{
var context = new StoreDBEntities();
context.CartItems.Add(CartItemToAdd);
return context.SaveChanges() > 0;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
堆栈跟踪器:
StackTrace "在 DataAccess.Dal.addCartItem(CartItem CartItemToAdd) 中 c:\Users\User\Documents\Visual Studio 2013\Projects\OrGarden\DataAccess\Dal.cs:406 行\r\n 在 BusinessLogicBackend.BLBackend.addCartItem(CartItem cartItemToAdd) 在 c:\Users\User\Documents\Visual Studio 2013\Projects\OrGarden\BusinessLogicBackend\BLBackend.cs:line 293\r\n 在 SyncInvokeaddCartItem(Object , Object[] , Object[] )\r\n 在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象 实例,Object[] 输入,Object[]& 输出)\r\n at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)" 字符串
重要更新:当我删除此分配Product = temp, 时,addCartItem 调用成功保存,没有问题。只有字段 Product 保持 null。
修改后的代码:
Product temp = business.getAllProducts().SingleOrDefault(
p => 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);
当我从上下文中检索 CrartItem 时,字段 Product 仍然是 null。
谁能帮我解决这个糟糕的问题?
非常感谢, 罗恩
【问题讨论】:
-
第一个问题是,恕我直言,您正在混合 2 个上下文:可能一个在
BLFrontend().getAllProducts()另一个在BLFrontend().addCartItem(cartItem)。 -
错误似乎
context没有 CartItems 属性。 (您的代码似乎也证实了这一点,尽管我看不到DbContext类)。如果您使用StoreDBEntities context而不是var context,智能感知应该会向您指出这一点。 -
@tschmit007 如果两个上下文出错,我必须更改什么?在 Dal 中 getAllProducts() 的代码是:
public List<Product> getAllProducts() { try { List<Product> ls = new StoreDBEntities().Products.ToList(); return ls; } catch (Exception ex) { throw new Exception(ex.Message); } } -
@HW 您可以在上面的 BusinessEntitys 中看到 CartItems 属性。我也试过你的建议,不幸的是它没有解决我的问题。谢谢!
-
上下文实例不应该是方法的一部分,而是(例如)类的构造函数
标签: c# asp.net entity-framework wcf ado.net