解决方案:存储更新、插入或删除语句影响了意外的行数 (0)

我为遇到异常的人找到了解决方案:

I found a solution for people who get an exception:

存储更新、插入或删除语句影响了意外的行数 (0).自加载实体以来,实体可能已被修改或删除.刷新 ObjectStateManager 条目.

但是,无论如何我有疑问.

But, anyway I have question.

我阅读了主题:实体框架:存储更新、插入、或删除语句影响了意外的行数 (0)."致VMAtm,罗伯特·哈维

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

我有触发器:

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?

推荐答案

解决方案:

try {
    context.SaveChanges();
} catch (OptimisticConcurrencyException) {
    context.Refresh(RefreshMode.ClientWins, db.Articles);
    context.SaveChanges();
}

相关文章