Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework 4.1 Context in Extension Method
    primarykey
    data
    text
    <p>When I run this code it says that:</p> <pre><code>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. </code></pre> <p>I can imagine the situation here. Probably because of this:</p> <pre><code>//Default.aspx.cs MyEntity cont = new MyEntity(); IEnumerable&lt;Product&gt; p = from c in cont.Products.AsEnumerable() select c; Product example = p.ToArray()[0]; example.Delete(); // This Delete method is an extension method which you'll see in below. //I also have some other stuff like IEnumerable&lt;Category&gt; cc = from n in cont.Categories.AsEnumerable() select n; cont.Categories.Remove(cc.ToArray()[0]); cont.SaveChanges(); //throws the error </code></pre> <p>Here is the main part of the extension method</p> <pre><code>public static bool Delete(this Product pro,bool SetNull = false) { using (var en = new MyEntity()) { IEnumerable&lt;Product&gt; s = from ss in en.Products where ss.ID == pro.ID select ss; en.Products.Remove(s.SingleOrDefault()); en.SaveChanges(); } } </code></pre> <p>The extension methods works, the entity removes from db. But when the program comes to the</p> <pre><code>cont.SaveChanges(); </code></pre> <p>line , it throws the error. I assume, this error comes up because i've changed the entities within some another method using another MyEntity() class. So i've searched to find a way to refresh the other one. But i couldn't ...</p>
    singulars
    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.
    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