商店更新,插入或删除语句影响的行的一个意外的数字(0)的EntityFramework [英] Store update, insert, or delete statement affected an unexpected number of rows (0) EntityFramework

查看:206
本文介绍了商店更新,插入或删除语句影响的行的一个意外的数字(0)的EntityFramework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到休耕的错误,当我尝试保存到一个环境中所做的更改

I keep getting the fallowing error when i try to save changes made to a context

商店更新,插入或删除语句影响了意想不到
  行数(0)。实体可能已被修改或自删除
  实体被加载。刷新ObjectStateManager条目。

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.

我有休耕类。

I have the fallowing classes. Person

public class Person : IPerson
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Name
    {
        get
        {
            return FirstName + " " + LastName;
        }
        set{}
    }

    public string Email { get; set; }
    public DateTime? LastModified { get; set; }
    public virtual ICollection<Result> Results { get; set; }
}

用户配置

public class UserProfile : Person
{
    public UserProfile()
    {
        Faculty = new Faculty();
        Projects = new Collection<Project>();
        Results = new Collection<Result>();
    }
    public string UserName { get; set; }
    public string CNP { get; set; }
    public virtual Faculty Faculty { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
}

结果

public abstract class Result:INamedEntity
{
    protected Result()
    {
        ResultType = new ResultType();
    }
    public int Id { get; set; }
    public string Name{get;set;}
    public virtual ResultType ResultType { get; set; }
    public virtual ICollection<Person> People { get; set; }
    public DateTime? LastModified { get; set; }
}

在我使用的是值添加到上下文

After i add a value to the context using

_ctx.Users.Single(u => u.Id == userId).Results.Add(result);

我得到的错误,当我打电话 _ctx.SaveChanges()

更新我的功能

public bool Save()
{

    try
    {
        _ctx.SaveChanges();
    }
    catch (OptimisticConcurrencyException)
    {
        ((IObjectContextAdapter)_ctx).ObjectContext.Refresh(RefreshMode.ClientWins,_ctx.Users);
        ((IObjectContextAdapter)_ctx).ObjectContext.Refresh(RefreshMode.ClientWins,_ctx.Results);
        _ctx.SaveChanges();
    }
    return true;
}

但没有被捕获的错误。
谢谢

But the error isn't caught. Thank you

推荐答案

想通了。
问题是,有我们两个人在这个项目上的工作,和我的同事创建了自己的的DbContext 的一个实例,他正在更新一个,我其他的。因此,对于谁曾经在将来出现此问题,确保你没有在同一时间拥有你的的DbContext 两个不同的实例

Figured it out. The problem was that there's two of us working on this project, and my colleague created his own instance of DBContext he was updating one and i the other. So for who ever has this problem in the future make sure you don't have two different instances of your DBContext at the same time

这篇关于商店更新,插入或删除语句影响的行的一个意外的数字(0)的EntityFramework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆