WCF and Entity Framework proxy objects

I'm developing a WCF RESTful service with an Entity Framework Code First 4.4.0.0 library, C# and .NET Framework 4.0.

I'm trying to retrieve the groups where user_id is member of with this method:

public List<Group> GetUserGroups(string user_id)
{
    List<Group> groups = null;
    int userId;
    OutgoingWebResponseContext ctx =
        WebOperationContext.Current.OutgoingResponse;

    // Check parameters. It will throw an exception.
    ParameterCheck.CheckObjectId(user_id);

    // Parase parameter to int.
    userId = Int32.Parse(user_id);

    try
    {
        using (var context = new MyContext())
        {
            //context.Configuration.ProxyCreationEnabled = false;
            //context.Configuration.LazyLoadingEnabled = false;

            User user = context.Users.Find(userId);

            if (user == null)
            {
                ThrowCustomWebException(
                    (int)ExceptionReasons.InvalidUser,
                    (int)ExceptionDetailedInformation.UserNotFound);
            }
            else
            {
                var grps = from g in context.Groups.Include("Users").Include("WantsToDo")
                           from u in g.Users
                             where u.UserId == userId
                           select g;

                if ((grps == null) ||
                    (grps.Count() == 0))
                {
                    ctx.StatusCode = System.Net.HttpStatusCode.NotFound;
                    ctx.SuppressEntityBody = true;
                }
                else
                {
                    foreach (Group group in grps)
                    {
                        group.UsersIds = group.Users.Select(u => u.UserId).ToList();
                        group.ActivityIds = group.WantsToDo.Select(a => a.ActivityId).ToList();
                    }
                    groups = grps.ToList();

                    ctx.StatusCode = System.Net.HttpStatusCode.OK;
                }
            }
        }
    }
    catch (Exception ex)
    {
        // ReThrow this exception because we need to indicate that the user is
        // invalid because there isn't that user on database.
        if ((ex is WebFaultException<ErrorData>))
            throw;
        else
        {
            TraceTool.WriteTraceLog(ex, "GetUserGroups");

            ctx.StatusCode = System.Net.HttpStatusCode.InternalServerError;
            ctx.SuppressEntityBody = true;
        }
    }

    return groups;
}

But, I have a problem with WCF because, here:

group.UsersIds = group.Users.Select(u => u.UserId).ToList();

group is a Proxy object.

But if I do context.Configuration.ProxyCreationEnabled = false; or context.Configuration.LazyLoadingEnabled = false;, then group is not a proxy but group.Users is null.

How can I fix this problem?

Answers


Try this:

var groups = from g in context.Groups           //Get all Groups in the database
                              .Include("Users") //Get the User data too
                              .Include("WantsToDo") //Get the WantsToDo data too
where g.Users.Any(u => u.UserId == user_id) //Only get groups where the user_id 
                                            //is in Users collection
select g

You don't need the Includes unless you intend to access that data later


Need Your Help

How to create site using SharePoint Designer 2013 and SharePoint Server 2013?

sharepoint sharepoint-2013

When i'm trying to create New 'Blank Site' in SharePoint Designer 2013 it tells me

Redrawing UITableViewCell when entering/exiting edit mode

iphone ios cocoa-touch uitableview

I have a table view in which the cells are built differently depending on whether the table is editing or not. Specifically, the selection style is none when in edit mode and blue when not in edit...