0

I am hoping someone can help me identify the cause of the following error when doing on update in Entity Framework.

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

From what I have read, it means that data has changed between the fetch and save, however this is not the case in my situation. (Single developer running the application locally. I also have viewed the data in the database before I attempt to save, and it is the exact same as when I did the fetch. Also able to reproduce this on command.)

I am not sure if this is relevant, but it seems to be the only factor that is different than other entities that are working. I have an entity that represents a table with a composite key. The value that I am updating is one of the values that makes up the composite key. There is only one record in the table at the moment, so I know there is no primary key violation stuff going on.

Does anyone know what steps I can take to find out what the actual problem is?

Thanks

Dave
  • 2,473
  • 2
  • 30
  • 55
  • I did some quick googling of "entity framework composite key updating", and I can't find anything definitive that says you can't update primary key fields, but I do see hints that people have had this difficulty as well. So I don't have an answer for you, but you may have better luck researching it with the suspicion that the composite key update is causing the problem. – Phil Sandler Jun 12 '14 at 17:44
  • Interesting that EF apparently allows you to change a PK value. This should not be possible, with or without child objects. (Expected exception `The property is part of the object's key information and cannot be modified.`). – Gert Arnold Jun 12 '14 at 21:36

4 Answers4

1

If you have instead of trigger on table you are inserting to, it causes this error. You have to rewrite trigger to after insert trigger, or at the end of trigger select new generated id. Maybe for update there is similar problem. Look at query entity framework generates - it can help you to see what's going on.

EDIT:

To see generated queries set logging:

public class CustomContext : DbContext
{
    public CustomContext()
        : base("name=CustomString")
    {
        // configure writing queries to console
        Database.Log = Console.Write;
    }

    // other context stuf ...
}

Or use some profiler (for sql server express you can use http://expressprofiler.codeplex.com/).

Fanda
  • 3,760
  • 5
  • 37
  • 56
0

The problem resides in the fact that you call the method _dataContext.SaveChanges();, but nothing changed in the data. To avoid this error try this:

public void EditCustomer(Customer customer)
    {
        _dataContext.Customer.Attach(customer);
        var entry = _dataContext.Entry(customer);

        if(entry.Property(e => e.DeviceId).CurrentValue != entry.Property(e => e.DeviceId).OriginalValue)
        {
            entry.Property(e => e.DeviceId).IsModified = true;
        }

        if(entry.Property(e => e.Name).CurrentValue != entry.Property(e => e.Name).OriginalValue)
        {
            entry.Property(e => e.Name).IsModified = true;
        }


        if(entry.Property(e => e.DeviceId).IsModified || entry.Property(e => e.Name).IsModified)
        {
            _dataContext.SaveChanges();
        }            
    }

I hope this helps you.

@DonPablone

Don Pablone
  • 51
  • 2
  • 7
0

I've encountered with such error message, my environment is as follows, SQL server 2016 along with ef6 database first and the issue was that the database developer did not define an identity seed column in the Id column of the table I'm inserting data into, and of course the issue solved when I updated the table design, so I'm sharing this experience in case if anyone got the same issue.

0

If by any chance, we have the same problem when trying to update record using Attach() and then SaveChanges() combination? This may help...

I am using SQLite DB and its EF provider (the same code works in SQLServer DB without problem).

I found out, when your DB column has GUID (or UniqueIdentity) in SQLite and your model is nvarchar, SQLIte EF treats it as Binary(i.e., byte[]) by default. So when SQLite EF provider tries to convert GUID into the model (string in my case) it will fail as it will convert to byte[]. The fix is to tell the SQLite EF to treat GUID as TEXT (and therefore conversion is into strings, not byte[]) by defining "BinaryGUID=false;" in the connectionstring (or metadata, if you're using database first) like so:

  <connectionStrings>
    <add name="Entities" connectionString="metadata=res://savetyping...=System.Data.SQLite.EF6;provider connection string=&quot;data source=C:\...\db.sqlite3;Version=3;BinaryGUID=false;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

Link to the solution that worked for me: How does the SQLite Entity Framework 6 provider handle Guids?

k3nn
  • 131
  • 1
  • 6