Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use this approach:</p> <pre><code>public class Company { public int Id { get; set; } public string Name { get; set;} private ICollection&lt;User&gt; _users; public ICollection&lt;User&gt; Users { get { return _users ?? (_users = new HashSet&lt;User&gt;()); } set { _users = value; } } } </code></pre> <p><code>HashSet</code> is better then other collections if you also override <code>Equals</code> and <code>GetHashCode</code> in your entities. It will handle duplicities for you. Also lazy collection initialization is better. I don't remember it exactly, but I think I had some problems in one of my first EF test applications when I initialized the collection in the constructor and also used dynamic proxies for lazy loading and change tracking.</p> <p>There are two types of entities: detached and attached. An attached entity is already tracked by the context. You usually get the attached entity from linq-to-entities query or by calling <code>Create</code> on <code>DbSet</code>. A detached entity is not tracked by context but once you call <code>Attach</code> or <code>Add</code> on the set to attach this entity all related entities will be attached / added as well. The only problem you have to deal with when working with detached entities is if related entity already exists in database and you only want to create new relation. </p> <p>The main rule which you must understand is difference between Add and Attach method:</p> <ul> <li><code>Add</code> will attach all detached entities in graph as <code>Added</code> => all related entities will be inserted as new ones.</li> <li><code>Attach</code> will attach all detached entities in graph as <code>Unchanged</code> => you must manually say what has been modified.</li> </ul> <p>You can manually set state of any attached entity by using:</p> <pre><code>context.Entry&lt;TEntity&gt;(entity).State = EntityState....; </code></pre> <p>When working with detached many-to-many you usually must use these techniques to build only relations instead of inserting duplicit entities to database.</p> <p>By my own experience working with detached entity graphs is very hard especially after deleting relations and because of that I always load entity graphs from database and manually merge changes into attached graphs wich are able to fully track all changes for me.</p> <p>Be aware that you can't mix entities from different contexts. If you want to attach entity from one context to another you must first explicitly detach entity from the first one. I hope you can do it by setting its state to <code>Detached</code> in the first context.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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