There are scenarios where we may want to add additional methods to an Entity class.

When we implement  them we have to extend the Entity Data Model Class project by implementing a partial class in our Entity Project. We then create a data service and reference the EDM class project . You can add custom behaviors on WCF Data Services and expose the methods on the Entity Class.

Below are the steps that we need to take in order to consume a custom method added to our Entity class from our WCF Data Services.

 

Way to Do it!

 

Suppose we add below Method in our Entity Class

=======================================

public partial class AdventureWorksEntities

    {

        public int contactid(stringfirstname)

        {

            AdventureWorksEntities aw = new AdventureWorksEntities();

            Contact contact = (from c in aw.Contact where c.FirstName == firstname select c).FirstOrDefault<Contact>();

            returncontact.ContactID;

        }

    }

Note we have added a new class file to the Entity Project and expanded that class.

Our DataServices class looks like below

DataServices Class

=============================

 

[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]

    public class DataService : DataService<EDM.AdventureWorksEntities>

    {

        public static void InitializeService(IDataServiceConfigurationconfig)

        {

            try

            {

                config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

                config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);

                config.UseVerboseErrors = true;

            }

 

            catch (Exceptione)

            {

                throw new DataServiceException(e.Message + "\n"+ e.StackTrace);

            }

        }

 

        [WebGet]

        public int ContactidTest(stringFirstName)

        {

            AdventureWorksEntities aw = new AdventureWorksEntities();

            if (string.IsNullOrEmpty(FirstName))

            {

                throw new ArgumentNullException("FirstName",

                          "You must provide a First name argument");

            }

 

            returnaw.contactid(FirstName);

        }

 

Consumer Application consumes DataServices as below

============================================

try

            {

                var proxy = new ServiceReference1.AdventureWorksEntities(new Uri("http://localhost:6102/DataService.svc/"));

                var contacts = proxy.Execute<int>(new Uri(string.Format("http://localhost:6102/DataService.svc/ContactidTest?FirstName='{0}'","gustavo")));

                strings = contacts.ToString();

                foreach (var contact incontacts)

                {

                    textBox1.Text = contact.ToString();

                }

            }

 

            catch (DataServiceQueryExceptionex)

            {

                throw new DataServiceException(ex.Message + "\n"+ ex.StackTrace);

            }

 

 

You will note in the above we are using the URI from Execute method of the DataServices proxy class.

There is other method (CreateQuery)of the proxy class which you can use in case the method in entity would have been returning an entity instead of a scalar value as above.

Link below gives elaboration as to how to use CreateQuery , Execute etc.

http://blogs.microsoft.co.il/blogs/gilf/archive/2008/11/14/consuming-data-services-service-operations.aspx


 Not all public method are exposed, unless you want them to be exposed using the WebGet attribute

 

 

Author : Ambuj (MSFT) , SQL Developer Technical Lead , Microsoft

Reviewed by : Bindesh (MSFT) , SQL Developer Engineer , Microsoft