需要我的插入代碼幫助。我收到一個錯誤無法插入實體框架
「對象引用未設置爲對象的實例」。
我對實體框架相當陌生。希望你們能幫助我。
這是代碼:
protected void SaveButton_Click(object sender, EventArgs e)
{
var context = new MHC_CoopEntities();
InventList product = new InventList
{
InventCategory = { CategoryID = 2 },
ItemName = "Del Monte Fit & Right Pineapple 330ml",
UnitQty = 48,
UnitPrice = (decimal) 20.85
};
context.AddToInventLists(product);
context.SaveChanges();
}
堆棧跟蹤:
在Coop_WebApp._Default.SaveButton_Click(對象 發件人,EventArgs e)在E:\其它\ Wabby KO \實體框架 4.0 \ EF_Soln \ Coop_WebApp \ Default.aspx.cs:線37
在System.Web.UI.WebControls.Button.OnClick(EventArgs的)
在System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 eventArgument)
在System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument)
在System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串eventArgument)
在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection中POSTDATA)
在System.Web.UI.Page.ProcessRequestMain(布爾 includeStagesBeforeAsyncPoint,布爾includeStagesAfterAsyncPoint)
你能發佈完整的異常消息+堆棧跟蹤嗎? – ysrb
問題現在已更新。 – Musikero31
你可以嘗試設置product.CategoryID = 2; ? – ysrb