VAAA
VAAA

Reputation: 15039

c# WCF DataService + EntityFramework custom stored procedure

I have a c# application that uses WCF DataServices with EntityFramework.

Seems everything is working fine, but now I need to implement a custom insert using a Stored Procedure.

I have tried to find any help online but seems there is nothing.

I have my Data Service:

[MessageInspectorBehavior]
    public class Security : DataService<VisitAwareContext>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            config.SetEntitySetAccessRule("SecurityUsers", EntitySetRights.All);
            config.SetEntitySetAccessRule("Sessions", EntitySetRights.All);
            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
            // Other configuration here...
            config.UseVerboseErrors = true; // TODO - Remove for production?
        }

[WebGet]
        public bool CustomInsert(string customField1, string customField2)
        {
            // here I need to call my stored procedure and pass the fields....

            return true;
        }
}

Is there any way to do that? Or someone can recommend me a solution?

I know that you can't stay just using one Server-Client data technology such WCF DataServices and I could add an alternate WCF ADO.NET approach to these scenarios but I would like to keep for the moment using EF + WCF Data Services.

Thanks a lot.

Upvotes: 0

Views: 1828

Answers (2)

tne
tne

Reputation: 7261

Use OData actions with WCF Data Services "Service Operations". As described in the MSDN article, you may use the WCF WebInvoke attribute on a method inside your data service class (respecting the constraints given by the attribute).

Inside the method, you can use the CurrentDataSource property of your data service (inherited from its base DataService class) to retrieve the Entity Framework DbContext.

From there, you may search Stack Overflow on how to call a stored procedure with the Entity Framework (or better; read the fine manual, of course).

Upvotes: 1

Pratik
Pratik

Reputation: 607

Instead of [WebGet] attribute, use [WebInvoke]. That way you should be able to call the service operation using POST instead of GET from the client. Apart from that, what you have seems correct.

Thanks Pratik

Upvotes: 0

Related Questions