Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>See the following code I use to easily attach a disconnected object back to the graph, assuming we're now going to save it.</p> <pre><code> public static class EntityFrameworkExtensions { /// &lt;summary&gt; /// This class allows you to attach an entity. /// For instance, a controller method Edit(Customer customer) /// using ctx.AttachAsModified(customer); /// ctx.SaveChanges(); /// allows you to easily reattach this item for udpating. /// Credit goes to: http://geekswithblogs.net/michelotti/archive/2009/11/27/attaching-modified-entities-in-ef-4.aspx /// &lt;/summary&gt; public static void AttachAsModified&lt;T&gt;(this ObjectSet&lt;T&gt; objectSet, T entity) where T : class { objectSet.Attach(entity); objectSet.Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); } /// &lt;summary&gt; /// This marks an item for deletion, but does not currently mark child objects (relationships). /// For those cases you must query the object, include the relationships, and then delete. /// &lt;/summary&gt; /// &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt; /// &lt;param name=&quot;objectSet&quot;&gt;&lt;/param&gt; /// &lt;param name=&quot;entity&quot;&gt;&lt;/param&gt; public static void AttachAsDeleted&lt;T&gt;(this ObjectSet&lt;T&gt; objectSet, T entity) where T : class { objectSet.Attach(entity); objectSet.Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted); } public static void AttachAllAsModified&lt;T&gt;(this ObjectSet&lt;T&gt; objectSet, IEnumerable&lt;T&gt; entities) where T : class { foreach (var item in entities) { objectSet.Attach(item); objectSet.Context.ObjectStateManager.ChangeObjectState(item, EntityState.Modified); } } } </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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