Solution for: Store update, insert, or delete stat

2019-01-05 00:46发布

I found a solution for people who get an exception:

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.

But, anyway I have question.

I read topic: Entity Framework: "Store update, insert, or delete statement affected an unexpected number of rows (0)." To VMAtm, Robert Harvey

In my case I had for example table articles:

Articles
------------
article_id
title
date_cr
date_mod
deleted

And I had trigger:

create trigger articles_instead_of_insert 
on articles instead of insert 
as      
    SET NOCOUNT ON;
    insert into articles(
        article_id, 
        title, 
        date_cr,
        date_mod, 
        deleted
    )
    select 
        user_id, 
        title, 
        isnull(date_cr, {fn NOW()}),
        isnull(date_mod, {fn NOW()}),
        isnull(deleted, 0)
    from inserted;
go

When I delete this trigger then I dont get this exception. So this trigger is problem. And now I have a question - Why? Should I do something?

9条回答
虎瘦雄心在
2楼-- · 2019-01-05 01:31

Its better you update your save method like this..... In case you calling savechange() method of entity context after every addobject and deleteobject or modification :

public void Save(object entity)
{
    using (var transaction = Connection.BeginTransaction())
    {
        try
        {
            SaveChanges();
            transaction.Commit();
         }
         catch (OptimisticConcurrencyException)
         {
             if (ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Deleted || ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Modified)
                    this.Refresh(RefreshMode.StoreWins, entity);
              else if (ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Added)
                    Detach(entity);
              AcceptAllChanges(); 
              transaction.Commit();
          }

    }
}
查看更多
爷、活的狠高调
3楼-- · 2019-01-05 01:31

It's because you have SET NOCOUNT ON.
The EF SQL Statement that it generates ALWAYS adds a clause of where @@ROWCOUNT > 0 and [ID] = scope_identity() (for example).
Notice the where @@ROWCOUNT > 0 clause. Take off your SET NOCOUNT ON statement and it should work.

查看更多
萌系小妹纸
4楼-- · 2019-01-05 01:34

I had the same problem, this error message is a rather mystifying one. The answer from webtrifusion helped me. See Entity Framework: "Store update, insert, or delete statement affected an unexpected number of rows (0)."

It turns out I forgot to add "Id:0" in the JSON on the client side.

查看更多
混吃等死
5楼-- · 2019-01-05 01:36

Possible Problem: You placed a ReadOnlyAttribute in you entity key's metadata, which causes its value to become zero

 [DisplayName("Student ID")]
 [ReadOnly(true)]
 public int StudentID { get; set; }

Solution: Remove the ReadOnlyAttribute

 [DisplayName("Student ID")]
 public int StudentID { get; set; }
查看更多
迷人小祖宗
6楼-- · 2019-01-05 01:40

Using Entity Framework Code first,

using (MyDbContext db = new MyDbContext ( )) {..

Adding this after the using line:

db.Configuration.ValidateOnSaveEnabled = true;
查看更多
▲ chillily
7楼-- · 2019-01-05 01:43

Solution:

try {
    context.SaveChanges();
} catch (OptimisticConcurrencyException) {
    context.Refresh(RefreshMode.ClientWins, db.Articles);
    context.SaveChanges();
}
查看更多
登录 后发表回答