When I attempt to edit a model in Entity Framework, I receive a "Unable to update the EntitySet since it includes a DefiningQuery..." error.

asp.net-mvc entity-framework exception linq-to-sql

Question

An exception is raised while changing an entity using LINQ to SQL and Entity Framework.

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

The update code 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 do I keep receiving this error?

1
24
7/21/2011 5:21:05 PM

Accepted Answer

The table's structure, One main key must be created in order to prevent the mistake., was the source of the issue. Update the edmx after that. The issue will be resolved

51
7/22/2011 4:37:22 AM

Popular Answer

Three items:

  1. Avoid catching exceptions that you cannot manage. Every exception that can be caught is being caught, but nothing is being done with it (except swallowing it). Do you really want to sit idly by if anything goes wrong? That's a Bad Thing. This results in a corrupted state that is difficult to troubleshoot. Very bad.

  2. Both Entity Framework and Linq to SQL are ORMs. Although you could be using LINQ to update the objects, Entity Framework is what you're really utilizing, not Linq to SQL (Linq to Entities).

  3. Have you tried the highlighted in this solution? If the exception you provided is the same, can you comment on whether or not the following works for you? The exception you supplied is slightly cut off, so I can't be sure it's precisely the same (please edit your post if it isn't).

"[..] 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)

(Referring to "Mike" on MSDN)



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow