How I keep entity object after DataContext disposed?
I use this style when I get data from database
public class User
{
public static List<User> GetUsers()
{
List<User> users = new Users();
using ( var context = new DataContext())
{
users = context.Users.ToList();
}
return users;
}
}
After I get data from database then I want to Filter user likes this
List<开发者_Go百科;User> userResultList = User.GetUsers();
userResultList.Where(u => u.IsActive == true);
But cannot filter and I get this error
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
My Question : Is possible way to filter result object after DataContext disposed?
Thank you very much for every support.
Update !!!
I think it possible if I create new DbContext in method scope that I want to retrieve data
Is it good enough approach?
Example
public class User
{
public static List<User> GetUsers()
{
List<User> users = new Users();
var context = new DataContext())
users = context.Users.ToList();
return users;
}
}
I wonder if you clone the list will get around the error?
User[] L = new User[users.Count];
users.CopyTo(L);
Speculation:
What you had looks like it should work on the surface.
I wonder if the thing that is returned by ToList() is something that inherits from list, and retains some hidden context awareness inside.
I can't test this idea right now. Something to think about for later though.
精彩评论