asp.net-mvc – 为什么我得到一个“无法更新EntitySet,因为它有一个定义查询…”异常时,试图更新在Entity框架中的模型?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 为什么我得到一个“无法更新EntitySet,因为它有一个定义查询…”异常时,试图更新在Entity框架中的模型?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在使用Entity Framework的LINQ to sql的帮助下更新时,抛出异常。
  1. System.Data.UpdateException: Unable to update the EntitySet 't_emp' because it has
  2. a DefiningQuery and no <UpdateFunction> element exists in the
  3. <ModificationFunctionMapping>

更新的代码是:

  1. public void Updateall()
  2. {
  3. try
  4. {
  5.  
  6.  
  7. var tb = (from p in _te.t_emp
  8. where p.id == "1"
  9. select p).FirstOrDefault();
  10. tb.ename = "jack";
  11.  
  12. _te.ApplyPropertyChanges(tb.EntityKey.EntitySetName,tb);
  13. _te.SaveChanges(true);
  14. }
  15. catch(Exception e)
  16. {
  17.  
  18. }
  19. }

为什么我收到此错误

解决方法

问题出在表结构中。为了避免错误,我们必须在表中创建一个主键。之后,更新edmx。问题将得到解决

猜你在找的asp.Net相关文章