Trying to find a correct implementation of EF6

Viewed 77

Enclosed below is an example of one of the methods that I created.
Am I correctly implementing EF6? As you can see in my commented code I first tried to create a repository class. I instead scrapped the repository class for this implementation.
However I am now getting errors because I am returning an IQueryable object and then closing the dbcontext.

So this led me to the question: Am I implementing EF6 correctly?
I could change the IQueryable to return a List<obj> or I could remove the using (_myContext) statement.

I'm just trying to understand the correct way to implement my methods.

public IQueryable<MY_USERS> GetAllUsers()
{
    using (_myContext)
    {
        return _myContext.MY_USERS;
    }
    //MyRepository<MY_USERS> users = new MyRepository<MY_USERS>(_myContext);
    //return users.GetAll();
}

Updated example:

public void DeleteUser(string userName)
{   
    using (var context = new MyEFConn())
    {
        using (var transaction = context.Database.BeginTransaction())
        {
            try
            {
                context.MY_USER_GROUPS.RemoveRange(GetUserGroups(userName));
                context.MY_USERS.Remove(new MY_USERS { USER_NAME = userName });
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw ex;
            }
        }
    }
}
1 Answers
Related