1

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;
1
  • You cant. If you know you are going to need the relation, you should load it eagerly. Or keep your context alive (that is if you want to get to the relation through the class). Commented Jun 29, 2013 at 21:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.