Note that there are some explanatory texts on larger screens.

plurals
  1. PODbUpdateConcurrencyException in Entity Framework Code First
    primarykey
    data
    text
    <p>I'm having a issue with Entity Framework and 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. When I try to delete the record I've just added, I get the following message:</p> <blockquote> <p>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.</p> </blockquote> <p>It seems to me that EF has no knowledge that this new record exists in database, despite the fact that it is there. 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.</p> <p>Does anyone knows why this is happening?</p> <p>Thanks in advance, Diego</p> <p><strong>EDIT</strong> I've assembled some code to make it easier to understand my problem:</p> <p>I have two simple entities:</p> <pre><code>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; } } </code></pre> <p>The Context:</p> <pre><code>public class DB : DbContext { public DB() : base("DB") { this.Configuration.AutoDetectChangesEnabled = true; this.Configuration.LazyLoadingEnabled = true; this.Configuration.ProxyCreationEnabled = true; } public DbSet&lt;Entidade1&gt; Entidade1 { get; set; } public DbSet&lt;Entidade2&gt; Entidade2 { get; set; } } </code></pre> <p>And the code:</p> <p>var item = new Entidade1();</p> <pre><code> 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload