Reputation: 22652
I have following EF code first code. It is working fine. But when I look for the value of 'club2.Members', it says following:
'club2.Members' threw an exception of type 'System.ObjectDisposedException'.
Message is:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
I have not set value for Members property. That’s okay. However it should not cause any exception internally.
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 exception?
namespace LijosEF
{
public class Person
{
public int PersonId { get; set; }
public string PersonName { get; set; }
public virtual ICollection<Club> Clubs { get; set; }
}
public class Club
{
public int ClubId { get; set; }
public string ClubName { get; set; }
public virtual ICollection<Person> 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<PluralizingTableNameConvention>();
}
public DbSet<Person> Persons { get; set; }
public DbSet<Club> Clubs { get; set; }
}
}
static void Main(string[] args)
{
Database.SetInitializer<NerdDinners>(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 => 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
}
}
Upvotes: 4
Views: 34773
Reputation: 22652
APPROACH 1
There is no need for the following method. This can be done in the present context. It is only a select.
GetClubs("club1");
APPROACH 2
Attaching the entities resolved the problem:
Club club1 = GetClubs("club1");
Club club2 = GetClubs("club2");
Club club3 = GetClubs("club3");
((IObjectContextAdapter)db).ObjectContext.Attach((IEntityWithKey)club1);
((IObjectContextAdapter)db).ObjectContext.Attach((IEntityWithKey)club2);
((IObjectContextAdapter)db).ObjectContext.Attach((IEntityWithKey)club3);
var test = club2.Members;
I should not have tried to refer club2.Members (of Club class), since it is not needed for creating Person object.
Members is related data in Club class. Since it is virtual, it will do lazy loading.
In a different scenario, if I had to retrieve Members object (which is a related data) from a different context, I should rely on eager loading (in the place which provides you with Club object).
Refer following also:
Upvotes: 1
Reputation: 13286
You're getting this error because you are disposing the dbcontext after you execute the query. If you want to use the data in "members" then load it with the query: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx
If you don't need lazy loading, which means that only data you're loading in the query is loaded (this will eliminate the exception), set LazyLoadingEnabled = false. Look here: Disable lazy loading by default in Entity Framework 4
Upvotes: 9