Asp.net-Mvc

為什麼在嘗試更新 Entity Framework 中的模型時出現“無法更新 EntitySet,因為它有一個 DefiningQuery…”異常?

  • July 21, 2011

在使用 Entity Framework 借助 LINQ to SQL 進行更新時,會引發異常。

System.Data.UpdateException: Unable to update the EntitySet 't_emp' because it has 
a DefiningQuery and no <UpdateFunction> element exists in the   
<ModificationFunctionMapping>

更新程式碼是:

public void Updateall()
   {
       try
       {


           var tb = (from p in _te.t_emp
                     where p.id == "1"
                     select p).FirstOrDefault();
           tb.ename = "jack";

           _te.ApplyPropertyChanges(tb.EntityKey.EntitySetName, tb);
           _te.SaveChanges(true);
       }
       catch(Exception e)
       {

       }
   }

為什麼我會收到此錯誤?

問題出在表結構中。為了避免錯誤,我們必須在表中創建一個主鍵。之後,更新 edmx。問題將得到解決

引用自:https://stackoverflow.com/questions/6774871