Using Stored procedures in Entity Model with a service

Thread is closed for posting
4 posts, 0 answers
  1. Andrew
    Andrew avatar
    1 posts
    Member since:
    Aug 2012

    Posted 09 Aug 2012 Link to this post

    I created an entity model of my database tables. I want to use the stored procedures i have generated and I was able to follow the help guides to create a domain method which I see gets generated correctly. My question is I went ahead and created a OpenAccess WCF Service. Now I cant figure out how I would be able to use that custom stored procedure from my client side application in code.
  2. Viktor Zhivkov
    Viktor Zhivkov avatar
    324 posts

    Posted 14 Aug 2012 Link to this post

    Hello Andrew,

    After mapping your Stored Procedures to Domain Methods in OpenAccess Designer you have to expose these methods through the WCF Service manually.
    For procedures that return primitive types (like string, integer, boolean and etc.) and entity types the procedure is straight forward:
    1. Add partial extension for the IEntitiesModel interface (the interface of the service contract).
    2. Create there a public method marked as [OperationContract] for each Stored Procedure you would like to expose to the service. Fill in the correct return type and the correct parameters.
    3. Add partial extension for EntitiesModel.svc.cs class (the implementation of your service contract). Implement all of the newly added methods from the service contract interface. You can use the Domain Methods mapped earlier (in my example GetLastErrorId is a stored procedure that accepts single int parameter and returns a single int value): 
      public int GetLastErrorId(int securityFlag)
          return this.unitOfWork.GetLastErrorId(securityFlag);
    4. Change IEntitiesModel.cs interface definition to include "partial" keyword. This step should be repeated after each execution of Add OpenAccess Service wizard, because the file gets regenerated each time. We will add the partial keyword to the generated code out of the box in a future release of OpenAccess.
    If you have Stored Procedures that return types that are not mapped in you data model things get complicated.
    You will have to manually create a DTO type for the generated "complex type", mark it as [DataContract], mark all of its properties as [DataMember], then add an assembler - in short recreate manually all the steps that Add OpenAccess Service wizard does for you.

    If you need any additional assistance do not hesitate post in our forums.

    Viktor Zhivkov
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  3. Patrick
    Patrick avatar
    14 posts
    Member since:
    May 2011

    Posted 23 Sep 2013 Link to this post

    Hi Viktor,

    we created a WCF webservice using OpenAccess from Telerik and it works all fine with entities and views.
    We are now trying to use some stored procedures but we are not able to create a webmethod to access them as webservices.

    What we've done sofar was creating a partial class to extend the EntitiesModelService with a method to call the stored procedure.
    We've already created a Domain Method for the stored procedure in the model.

    public partial class EntitiesModelService
            public IEnumerable<Store> FncGetNearbyPlaces(float? lat, float? lng, float? distance)
                return (new EntitiesModel()).FncGetNearbyPlaces(lat, lng, distance);

    and added this line in the EntitiesModelService

    public static void InitializeService(DataServiceConfiguration config)
       config.SetServiceOperationAccessRule("FncGetNearbyPlaces", ServiceOperationRights.All);

    Hope you can help us to find a solution soon.

  4. Viktor Zhivkov
    Viktor Zhivkov avatar
    324 posts

    Posted 26 Sep 2013 Link to this post

    Hi Patrick,

    Thank you for raising separate thread for your issue.
    You can find my answer in your other post at
    Please us the new forum thread for any further communication.

    Viktor Zhivkov
    OpenAccess ORM Q3 2013 Beta is available for immediate download in your account. Get it now and play with the latest bits. See what's new >>
Back to Top