I know that “join” is not supported on client side with WCF DS thats why i decided to add a method on server side to do the “join” and return the result as custom type object.
Service looks like this:
public class CWcfDataService : DataService<CEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
config.UseVerboseErrors = true;
config.RegisterKnownType(typeof(CASE_STAGE_HISTORY_EXTENDED));
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
[WebGet]
public IQueryable<CASE_STAGE_HISTORY_EXTENDED> GetCASE_STAGE_HISTORY_EXTENDEDByDocId(int docId)
{
CEntities context = new CEntities();
return (from c in context.CASE_STAGE_HISTORY
join a in context.USRs on c.CREATOR_ID equals a.USRID
select new CASE_STAGE_HISTORY_EXTENDED()
{
CASE_STAGE_ID = c.CASE_STAGE_HISTORY_ID,
CASE_STAGE_NAME = c.CASE_STAGE_NAME,
CREATE_DATE = c.CREATE_DATE,
CREATOR_ID = c.CREATOR_ID,
DOC_ID = c.DOC_ID,
LAST_VARIANT_DOCUMENT_ID = c.LAST_VARIANT_DOCUEMENT_ID,
CREATOR_FULLNAME = a.FULLNAME
});
}
}
And custom class is:
[DataServiceKey("CASE_STAGE_ID")]
public class CASE_STAGE_HISTORY_EXTENDED
{
public int CASE_STAGE_ID { get; set; }
public int DOC_ID { get; set; }
public string CASE_STAGE_NAME { get; set; }
public int? LAST_VARIANT_DOCUMENT_ID { get; set; }
public DateTime? CREATE_DATE { get; set; }
public int? CREATOR_ID { get; set; }
public string CREATOR_FULLNAME { get; set; }
}
When i try to update service reference in Visual Studio i constantly get error:
The server encountered an error
processing the request. The exception
message is ‘Unable to load metadata
for return type
‘System.Linq.IQueryable1[CWcf.Code.CASE_STAGE_HISTORY_EXTENDED]'1[CWcf.Code.CASE_STAGE_HISTORY_EXTENDED]
of method
'System.Linq.IQueryable
GetCASE_STAGE_HISTORY_EXTENDEDByDocId(Int32)’.’.
See server logs for more details.
If i remove the public IQueryable<CASE_STAGE_HISTORY_EXTENDED> GetCASE_STAGE_HISTORY_EXTENDEDByDocId(int docId) part – on updating service reference i get another error:
The server encountered an error
processing the request. The exception
message is ‘Internal Server Error. The
type
‘CourtWcf.Code.CASE_STAGE_HISTORY_EXTENDED’
is not a complex type or an entity
type.’.
Environment: Visual Studio 2010, .NET 4.
Added foreign keys and implemented LoadProperty. See the article from which i took this solution: http://thedatafarm.com/blog/data-access/the-cost-of-eager-loading-in-entity-framework/
Still if i have NO relations in database(for example i have no foreign keys)-i have another solution: create stored procedure and map it with import in your DB model. After that create complex type that will work in client too(tho you will have to access it by URI, not with lambda extensions). See example here.
Many thanks for other answerers in this thread, you have made me look deeper into the subject.