24

While updating with the help of LINQ to SQL using Entity Framework, an exception is thrown.

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

The code for update is :

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)
        {

        }
    }

Why am I getting this error?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Thomas Mathew
  • 1,151
  • 6
  • 15
  • 30

3 Answers3

51

The problem was in the table structure. To avoid the error we have to make one primary key in the table. After that, update the edmx. The problem will be fixed

Thomas Mathew
  • 1,151
  • 6
  • 15
  • 30
8

Three things:

  1. Don't catch exceptions you can't handle. You're catching every exception possible, and then doing nothing with it (except swallowing it). That's a Bad Thing™ Do you really want to silently do nothing if anything goes wrong? That leads to corrupted state that's hard to debug. Not good.

  2. Linq to SQL is an ORM, as is Entity Framework. You may be using LINQ to update the objects, but you're not using Linq to SQL, you're using Entity Framework (Linq to Entities).

  3. Have you tried the solution outlined here? The exception you posted is somewhat cut off, so I can't be sure it's exactly the same (please update your post if it isn't), and if it is the same, can you comment on whether or not the following works for you?

"[..] Entity Framework doesn't know whether a given view is updatable or not, so it adds the <DefiningQuery> element in order to safeguard against having the framework attempt to generate queries against a non-updatable view.

If your view is updatable you can simply remove the <DefiningQuery> element from the EntitySet definition for your view inside of the StorageModel section of your .edmx, and the normal update processing will work as with any other table.

If your view is not updatable, you will have to provide the update logic yourself through a "Modification Function Mapping". The Modification Function Mapping calls a function defined in the StorageModel section of your .edmx. That Function may contain the name and arguments to a stored procedure in your database, or you can use a "defining command" in order to write the insert, update, or delete statement directly in the function definition within the StorageModel section of your .edmx." (Emphasis mine, post formatted for clarity and for Stack Overflow)

(Source: "Mike" on MSDN)

George Stocker
  • 57,289
  • 29
  • 176
  • 237
  • Thanks for the help. The problem was in the table structure. After defining a primary key, the will not be any exception. – Thomas Mathew Jul 22 '11 at 04:39
  • Confirmed, need a PK on your table (a Composite PK will do). Deleting the DefiningQuery makes things worse. – StuartLC Aug 06 '12 at 07:11
0

But You can Set primary Key in Model if use MVC Asp.net

Just Open model.edmx in your table ,go to your field property and set Entity Key = True

porya_ras
  • 1
  • 1