Note that there are some explanatory texts on larger screens.

plurals
  1. POException of type 'System.ObjectDisposedException'
    primarykey
    data
    text
    <p>I have following EF code first code. It is working fine. But when I look for the value of 'club2.Members', it says following:</p> <blockquote> <p>'club2.Members' threw an exception of type 'System.ObjectDisposedException'.</p> </blockquote> <p>Message is:</p> <blockquote> <p>The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.</p> </blockquote> <p>I have not set value for Members property. That’s okay. However it should not cause any exception internally. </p> <ol> <li>Is there a way to overcome this exception? </li> <li>Does removal of this exception help in improving performance?</li> <li>Why this exception did not cause termination of program execution?</li> </ol> <p>Note: I don't need the Members data in club entity (at the point where I have called). Even there is no members added to the club at that time. All i need is to get rid of the exception; not to load data using eager load. How to avoid the <strong>exception</strong>? </p> <p><img src="https://i.stack.imgur.com/p8XdD.jpg" alt="enter image description here"></p> <pre><code>namespace LijosEF { public class Person { public int PersonId { get; set; } public string PersonName { get; set; } public virtual ICollection&lt;Club&gt; Clubs { get; set; } } public class Club { public int ClubId { get; set; } public string ClubName { get; set; } public virtual ICollection&lt;Person&gt; Members { get; set; } } //System.Data.Entity.DbContext is from EntityFramework.dll public class NerdDinners : System.Data.Entity.DbContext { public NerdDinners(string connString): base(connString) { } protected override void OnModelCreating(DbModelBuilder modelbuilder) { //Fluent API - Plural Removal modelbuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;(); } public DbSet&lt;Person&gt; Persons { get; set; } public DbSet&lt;Club&gt; Clubs { get; set; } } } static void Main(string[] args) { Database.SetInitializer&lt;NerdDinners&gt;(new MyInitializer()); CreateClubs(); CreatePersons(); } public static void CreateClubs() { string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30"; using (var db = new NerdDinners(connectionstring)) { Club club1 = new Club(); club1.ClubName = "club1"; Club club2 = new Club(); club2.ClubName = "club2"; Club club3 = new Club(); club3.ClubName = "club3"; db.Clubs.Add(club1); db.Clubs.Add(club2); db.Clubs.Add(club3); int recordsAffected = db.SaveChanges(); } } public static Club GetClubs(string clubName) { string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30"; using (var db = new NerdDinners(connectionstring)) { var query = db.Clubs.SingleOrDefault(p =&gt; p.ClubName == clubName); return query; } } public static void CreatePersons() { string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30"; using (var db = new NerdDinners(connectionstring)) { Club club1 = GetClubs("club1"); Club club2 = GetClubs("club2"); Club club3 = GetClubs("club3"); //More code to be added (using db) to add person to context and save } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    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