0

我有一个名为 的表PRODUCT,其中Price存储了 Product 的 。因为我知道产品的价格可以随时更改,所以我有一个名为 的表ORDER,其中包含Quantity某人正在订购的产品TodaysPrice的价格,以及应该从列Price中提取的价格交易完成的时刻。

问题是,我不知道如何提取属性的值并将该值分配给 LightSwitch 中的另一个属性。


到目前为止,我设法将其包含在Order_Created()

this.Order.SelectedItem.Price = this.Order.SelectedItem.Product.Price;

但是上面的代码不起作用,我得到了这个异常:

NullReferenceException was unhandled by user code

我不确定这是否正确,任何帮助将不胜感激。我想知道如何使用 Visual Studio 2010 在 LightSwitch 2011 中执行此操作。

提前非常感谢!

4

1 回答 1

0

当SelectedItemProduct当前没有值时,您的代码可能正在运行。在引用实体的任何属性之前,您应该始终检查 null。

像这样(注意在引用该实体的任何属性之前如何检查每个实体:

if (this.Order != null) 
    && (this.Order.SelectedItem != null) 
    && (this.Order.SelectedItem.Product != null)
{
    this.Order.SelectedItem.Price = this.Order.SelectedItem.Product.Price;
}

*Order_Created* 方法不是拥有此代码的正确位置,因为在创建订单时,您还不知道要选择哪个产品。The correct place to put this code is in the *Product_Changed* method, so that when the Product is selected (or changed), the Product 's current price then gets enetered in the Order .

另外一点,除非您为示例简化了表格,否则我认为您缺少表格。订单通常会有关联的Customer Date内容。它还会有OrderLines(或类似名称)的集合。OrderLine应该有产品/数量/价格详细信息,而不是订单本身。

于 2012-11-30T08:23:46.520 回答