Reputation: 834
I am using the code below to log in a user in my winform app
public static Boolean Attempt(String username, String password, bool salted = false)
{
using (InventorySystemEntities context = new InventorySystemEntities(new ConfigurationManager().ConnectionString))
{
password = password.ToMD5(salted);
User = context.Users.SingleOrDefault(u => u.UserName == username
&& u.Password == password && u.IsEnabled == true);
return User != null ? true : false;
}
}
is there a way to access data after the context has been disposed? like using a new context?
User test = Auth.Attempt(txtUsername.Text, txtPassword.Text);
//is there a way to access this?
test.UserGroup.Name;
Upvotes: 1
Views: 933
Reputation: 177133
You could use explicit loading to fetch the navigation property with a new context:
public static void LoadUserGroup(User user)
{
using (InventorySystemEntities context = new InventorySystemEntities(
new ConfigurationManager().ConnectionString))
{
context.Users.Attach(user);
context.Entry(user).Reference(u => u.UserGroup).Load();
}
}
Then you can access properties of the UserGroup
:
User test = Auth.Attempt(txtUsername.Text, txtPassword.Text);
//...
Auth.LoadUserGroup(test);
test.UserGroup.Name;
Without creating a new context it is not possible to navigate to the UserGroup
.
Upvotes: 1