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

Adobe CQ: authoring content once to display in multiple components

content-management-system adobe cq5

Is there any way to create components in CQ with content pre-added, so that authors can easily pull in a component that has the exact same content on each page where it appears?

HDFS Home Directory

hadoop cluster-computing hdfs user-permissions

I have setup a single node multi-user hadoop cluster.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.