簡體   English   中英

實體框架5 Linq插入查詢:操作無效

[英]Entity Framework 5 Linq Insert Query: Operation is invalid

嘗試執行簡單的linq插入查詢時出現錯誤。

例外:由於對象的當前狀態,操作無效。

這是我的linq查詢:

private void addEntry(string hqname, string hqaddress, string hqcity,
                      int hqzip, string hqstate, string hqcountry)
{
    using (var db = new dbcontext())
    {
        try
        {
            var dbObj = new dbObj();
            dbObj.HQName = hqname;
            dbObj.HQAddress = hqaddress;
            dbObj.HQCity = hqcity;
            dbObj.HQZip = hqzip;
            dbObj.HQState = hqstate;
            dbObj.HQCountry = hqcountry;
            db.tblName.Add(dbObj);
            db.SaveChanges();
        }
        catch
        {
            throw new InvalidOperationException();
        }
    }
}

這是我調用此方法的地方:

addCompanyHeadquarters(tbCompanyName.Text, addressInput.HQAddress, addressInput.HQCity, Convert.ToInt32(addressInput.HQZip), addressInput.HQState, "United States"); 

堆棧跟蹤:

[InvalidOperationException:操作由於對象的當前狀態而無效。 .UI.WebControls.Button.OnClick(EventArgs e)+9692746 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)+108 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent (字符串eventArgument)+12 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串eventArgument)+15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,布爾值includeStagesAfterAsyncPoint)+3562

我已經嘗試調試了一段時間,這個錯誤確實使我陷入了循環。 我不確定接下來要看什么。 任何前進的技巧都將不勝感激。 一些信息:我不得不將id數據庫列更新為自動增量,然后在更改了相應表和列StoredGeneratedPattern: Trueedmx文件StoredGeneratedPattern: True

更新:根據Egor Semenenko的建議,我添加了更改我拋出的異常的方法 :這是我的新堆棧跟蹤: 實體類型不是當前上下文模型的一部分 [InvalidOperationException:實體類型tblName不是當前上下文模型的一部分。

System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType) +196
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +38
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +52
   System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15
   System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) +64
   System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) +108
   System.Data.Entity.DbSet`1.Add(TEntity entity) +42
  addCompanyHeadquarters(String hqname, String hqaddress, String hqcity, Int32 hqzip, String hqstate, String hqcountry) in 
   Button3_Click(Object sender, EventArgs e) 
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9692746
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3562 

實際的例外是什么? 由於您正在使用try catch並拋出自己的InvalidOperationException,因此無法看到實際的異常。 由於對象的當前狀態,操作無效-只是InvalidOperationException的一般錯誤消息。

謝謝大家的幫助。 我們解決了這個問題,在ssdl文件中,實體集不存在,無法將數據庫列映射到實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM