简体   繁体   English

实体框架代码中的DbUpdateConcurrencyException优先

[英]DbUpdateConcurrencyException in Entity Framework Code First

I'm having a issue with Entity Framework and Code First. 我遇到了Entity Framework和Code First的问题。 I have an entity with a Timestamp property, and I add an new record to the database, call SaveChanges, and it's all ok. 我有一个具有Timestamp属性的实体,我向数据库添加一条新记录,调用SaveChanges,一切正常。 When I try to delete the record I've just added, I get the following message: 当我尝试删除刚刚添加的记录时,我收到以下消息:

Store update, insert, or delete statement affected an unexpected number of rows (0). 存储更新,插入或删除语句会影响意外的行数(0)。 Entities may have been modified or deleted since entities were loaded. 自实体加载后,实体可能已被修改或删除。 Refresh ObjectStateManager entries. 刷新ObjectStateManager条目。

It seems to me that EF has no knowledge that this new record exists in database, despite the fact that it is there. 在我看来,EF并不知道这个新记录存在于数据库中,尽管它存在。 Sometimes, even when I try to update an different record I get the same message, but if I try to delete an different one it works. 有时,即使我尝试更新不同的记录,我也会得到相同的消息,但是如果我尝试删除不同的记录,它就会起作用。

Does anyone knows why this is happening? 有谁知道为什么会这样?

Thanks in advance, Diego 先谢谢,迭戈

EDIT I've assembled some code to make it easier to understand my problem: 编辑我已经汇编了一些代码,以便更容易理解我的问题:

I have two simple entities: 我有两个简单的实体:

public class Entidade1
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public string Descricao { get; set; }

    [Timestamp]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public byte[] RecTS { get; set; }
}

public class Entidade2
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public string Descricao { get; set; }

    [Timestamp]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public byte[] RecTS { get; set; }

    public virtual Entidade1 Entidade1 { get; set; }
}

The Context: 上下文:

public class DB : DbContext
{
    public DB() : base("DB")
    {
        this.Configuration.AutoDetectChangesEnabled = true;
        this.Configuration.LazyLoadingEnabled = true;
        this.Configuration.ProxyCreationEnabled = true;
    }

    public DbSet<Entidade1> Entidade1 { get; set; }
    public DbSet<Entidade2> Entidade2 { get; set; }
}

And the code: 和代码:

var item = new Entidade1(); var item = new Entidade1();

        item.Descricao = "teste";

        var db = new DB();

        db.Set(typeof(Entidade1)).Add(item);

        db.SaveChanges();

        var item2 = new Entidade2();

        item2.Descricao = "teste 2";
        item2.Entidade1 = item;

        db.Set(typeof (Entidade2)).Add(item2);

        db.SaveChanges();

        var q = (from c in db.Entidade1
                 where c.Descricao == "teste"
                 select c).FirstOrDefault();

        db.Set(typeof(Entidade1)).Remove(q);

        db.SaveChanges();

        var q2 = (from c in db.Entidade2
                  where c.Descricao == "teste 2"
                  select c).FirstOrDefault();

        db.Set(typeof (Entidade2)).Remove(q2);

        db.SaveChanges(); // Here I got the error

I've found out that the problem is a known bug in Entity Framework 4.0, and that it's presente since 2010 and will be resolved in the next version (hope in 4.5). 我发现问题是Entity Framework 4.0中的一个已知错误,并且自2010年以来它已经出现并将在下一个版本中得到解决(希望在4.5中)。 When I update an object that has related objects, EF tries to update all the relations, and as nothing has changed in the parentes, it will give me the '0 rows' message. 当我更新具有相关对象的对象时,EF会尝试更新所有关系,并且由于parentes中没有任何更改,它将为我提供“0行”消息。

Hope it helps someone. 希望它可以帮助某人。

I suggest, using local scope for Context: 我建议,使用Context的本地范围:

 Using(var x = new MyObjectContext){ // //.... x.SaveChanges(); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM