I am trying to return a custom class from my wcf service. My codes are below and getting error.
The server encountered an error processing the request. The exception message is ‘The property ‘UserList’ on type ‘DealerModel’ is not a valid property. Properties whose types are collection of primitives or complex types are not supported.’. See server logs for more details. The exception stack trace is:
I am trying to get a json value like below;
<Dealer>
<DealerName/>
<DealerAdress/>
<Users>
<FirstName/>
<LastName/>
<FirstName/>
<LastName/>
</Users>
</Dealer>
My codes
using System;
using System.Linq;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
public class DataModel:IDisposable
{
private xModel.xEntities _dbContext = new xSocialModel.xEntities();
public DataModel()
{
Users = from e in _dbContext.Users
select new UserModel
{
UserID = e.UserID,
Firstname = e.Firstname,
Lastname = e.Lastname
};
Dealers = from e in _dbContext.Dealers
select new DealerModel
{
DealerID = e.DealerID,
DealerName = e.Name,
DealerAddress = e.Address,
UserList = e.Users
};
}
/// <summary>Returns the list of users.</summary>
public IQueryable<UserModel> Users { get; private set; }
public IQueryable<DealerModel> Dealers { get; private set; }
void IDisposable.Dispose()
{
_dbContext.Dispose();
}
}
using System;
using System.Data.Services.Common;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.ServiceModel;
using System.Data.Objects.DataClasses;
using System.Linq;
/// <summary>Represents a User.</summary>
[DataServiceKey("UserID")]
public class UserModel
{
public Guid UserID { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
[DataServiceKey("DealerID")]
public class DealerModel
{
public Guid DealerID { get; set; }
public string DealerName { get; set; }
public string DealerAddress { get; set; }
**public EntityCollection<xModel.User> UserList { get; set; }**
}
I had a lot of trouble with this as well. Projection doesn’t seem to work as nicely as RIA services used it. I’ll go ahead and assume you are using Entity Framework. The only way I’ve found to do this is to create Complex Types in your edmx.
To do this, open your EDMX, then select the Model Browser in the right pane. Under the “Model” node, you will see “Complex Types”, right click and select “Create Complex Type”. In the same window then, you can name your type and give it various properties of canned types.
From your question it appears you have a property that is a list. I have not been able to create these complex types with properties that contain other entities. Not saying it isn’t possible just that I haven’t been able to figure it out.
Now in your DataService, create a service method with a return type of your new complex type. In the service method, use projection to fill the object (or list) and return it as you would any other entity. On the front end you will need to call it like a Service Method instead of an entity, but otherwise the JSON will look similar to other entity queries.
Sam Mueller has a very nice blog on using this technique, check it out: http://samuelmueller.com/2009/11/working-with-projections-and-dtos-in-wcf-data-services/