Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would do the second option. Just there is no need to detach every single entity, you could set the query to NoTracking and entites will not be attached to the context in first place, and you would not loose the relationships (if loaded).</p> <pre><code> public static List&lt;MyParentEntity&gt; GetMyParentEntity(){ using (var context = new MyObjectContext(blablabla...)) { var resultSet = from e in context.MyParentEntitySet select e; ((ObjectQuery)resultSet).MergeOption = MergeOption.NoTracking; return resultSet.ToList(); }} </code></pre> <p>Second,the question is if your application is web based or windows based. If it's web based I recommend using of Singleton pattern where context would be created for each request.</p> <pre><code> public class Singleton { public static YourContext _context; public static YourContext Context { get { //We are in a web app, use a request scope if (HttpContext.Current != null) { YourContext current_context = (YourContext)HttpContext.Current.Items["_YourContext"]; if (current_context == null) { current_context = new YourContext(); HttpContext.Current.Items.Add("_YourContext", current_context); } return current_context; } else { if (_context == null) _context = new YourContext(); return _context; } } } } </code></pre> <p>I'm not sure what to do what is the best practice if it is not web app, so putting it in static field might not be the good thing. This framework is a bit complicated and reading how it works in depth would help to understand it better and avoid this kind of situations. I used Julia Lerman's book Programming Entity Framework, 1st Edition.</p>
 

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