5

How to perform a service-side join operation in WCF Data Services

 2 years ago
source link: https://www.codesd.com/item/how-to-perform-a-service-side-join-operation-in-wcf-data-services.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to perform a service-side join operation in WCF Data Services

advertisements

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]' of method 'System.Linq.IQueryable1[CWcf.Code.CASE_STAGE_HISTORY_EXTENDED] 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.


I assume the data service is based on an Entity Framework model (the CEntities class is an ObjectContext). If that's the case the types are completely read from the EF model (CSDL) and the class definitions are more or less ignored. So you need to define the type returned by your service operation in your EF model. Also note that for the IQueryable to work, that type has to be recognized by EF as an entity type (which might require mapping to the database, or something, EF experts will know more).


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK