Item
複数を含めることができますSizes
。アイテムに新しいサイズを追加しようとすると、NullReference
エラーが発生します。アイテムに画像を追加しようとすると、同じことが起こります。
オブジェクト参照がオブジェクト インスタンスに設定されていません。
コード
var size = new Size(){
BasePrice = currentBasePrice, // not null, checked in debugger
DiscountPrice = currentDiscountPrice // not null, checked in debugger
};
// item is not null, checked in debugger
item.Sizes.Add(size); // nothing here is null, however it throws null reference error here
アイテムモデル
public class Item
{
public int ID { get; set; }
public int CategoryID { get; set; }
virtual public Category Category { get; set; }
virtual public ICollection<Size> Sizes { get; set; }
virtual public ICollection<Image> Images { get; set; }
}
サイズモデル
public class Size
{
public int ID { get; set; }
public int ItemID { get; set; }
virtual public Item Item { get; set; } // tried to delete this, did not help
public decimal BasePrice { get; set; }
public decimal? DiscountPrice { get; set; }
}