需要我的插入代码帮助。我收到一个错误无法插入实体框架
“对象引用未设置为对象的实例”。
我对实体框架相当陌生。希望你们能帮助我。
这是代码:
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