Stored Proc and WCF Data Services - sql

I've got a WCF Data Service setup and can access the table data through the browser url.
Have created a simple Stored Proc which takes in a parameter and then returns some columns from various tables via Joins - how can I consume this?

If you are using an Entity Framework model you could do this:
Open the Model Browser.
Right-click "EntityContainer: (name)"-> "Function Imports" and select "Add Function Import...".
Select stored procedure, specify "Complex" as "Returns a Collection Of", click "Get Column Information" and click "Create New Complex Type".
Add config.SetServiceOperationAccessRule("SomeStoredProcedure", ServiceOperationRights.AllRead); to the InitializeService method in SomeDataService.svc.cs.
Now add a method with the WebGet attribute to SomeDataService.svc.cs that returns an IQueryable of the complex type you defined previously:
[WebGet]
public IQueryable<SomeStoredProcedure_Result> SomeStoredProcedure()
{
return CurrentDataSource.SomeStoredProcedure(1).AsQueryable();
}
The 1 above is a parameter to the stored procedure.
The stored procedure can now be consumed at:
http://localhost/SomeDataService.svc/SomeStoredProcedure

Related

Constructors with different scopes

In Windev, is it possible to declare multiple constructors with different scopes? Seems not.
For example:
PROCEDURE CONSTRUCTOR (param1, param2)
...
PROCEDURE PROTECTED CONSTRUCTOR (paramx, paramy, paramz)
...
When I'm trying to do things this way it gives me an error saying that attributes public, protected and private has to be the same between syntaxes of a same procedure.
To add a syntax to an existing procedure: In the project explorer,
select the procedure. Display the popup menu of the procedure and
select "Add a syntax". A new syntax is automatically created in the
code editor.
look at this :
https://doc.windev.com/en-US/?1514063&name=surcharge_prototypemultisyntaxe&1514063&name=surcharge_prototypemultisyntaxe
Hope it helps !

Control AIF document service schema

On Dynamics AX 2012 R3 CU8 when you use the wizard to create a document service, the system generates the schema for the different operations in the service. Is there a way to control what gets generated?
For example, if I create a query with HcmWorker as the parent and DirPerson as the child with just a few fields that I'm interested in, the system generates the schema with a few things I don't like, out of which I'll mention a couple below:
It adds fields like AxdEntity_DirPerson_DirParty.Name even though I explicitly didn't include this field in the query
The minOccurs on this field is 1, which doesn't work because it is a computed field. I prefer that this field is not included. If that is not possible, at least I would like to have minOccurs = 0
To make matters even more intriguing, the standard service (HcmWorkerImportService) for importing workers has the minOccurs = 0 for the Name field.
I'm trying to figure out how to control these values.
Have a look into the initMandatoryFieldsMap method from the AxdBase class and overwrite it if needed in your HcmWorkerImportService.
The initMandatoryFieldsMap method specifies which fields are mandatory
in the XML that the document class sends or receives. This method is
used to specify mandatory fields for the document without specifying
them at the table level.
See: MSDN: Walkthrough: Creating a Service Using the AIF Document Service Wizard ("To override the initMandatoryFieldsMap method")
Example:
protected void initMandatoryFieldsMap()
{
super();
this.setParmMethodAsMandatory(classnum(AxdSalesOrder),
methodstr(AxdBase,parmDocPurpose));
}
See: AxdBase.initMandatoryFieldsMap Method

Linq with entities

I am starting to develop a system using silverlight 5 with wcf ria. I use Visual studio 2010 and the code is in visual basic. In my application I have a table guiasidiomas. It is a very simple table:
Id
Idguia
Ididioma
In the application I changed the query getguiasidiomas that is generated by the domain service
Public Function GetGuiasIdiomas(ByVal idProcurado As String) AsIQueryable(Of GuiasIdioma)
Return Me.ObjectContext.GuiasIdiomas.Where(Function(c) (c.idGuia).ToString = idProcurado)
End Function
I inserted a where in order to filter by a given idguia.
That is the query that has a problem.
If you could help me I would appreciate very much.
When I run the query I get:
SCRIPT5022: Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.ServiceModel.DomainServices.Client.DomainOperationException: Load operation failed for query 'GetGuiasIdiomas'. LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
Linq-to-Entities cannot translate .ToString() directly to SQL, so you cannot use it in your WHERE clause. Here are two options:
Convert idProcurado to the same datatype as idGuia and compare directly
Pull ALL entities from the database (e.g. by calling .AsEnumerable()) and doing the comparison against the object list.
I'm guessing 1) is going to give you better performance by only puling back one or a small number of entities.
As your error message says, linq to entities does not support ToString.
You can prepare list of strings before using it with linq to entities.

Entity Framework Dynamic Mapping

I have a EntityAttributeValue database that I have no control over. To get the data I use a stored proc as follows.
public class PhotoDataContext : DbContext, IPhotoDataContext
{
public IEnumerable<PhotoRegistration> GetPhotoRegistration()
{
return this.Database.SqlQuery<PhotoRegistration>("SP_Photo") ;
}
}
This works fine for when the POCO maps perfectly. Now I have a stored procedure that can return dynamic fields. All fields are of string type.
I am using EF 4.1 Code First and am passing the POCO's to Silverlight via WCF.
Any ideas how to stop the auto mapping of EF and to pivot the data back into a name value pair that WCF will be happy to serialize.
Thanks
J
Use ADO.NET = SqlConnection, SqlCommand, SqlDataReader (or equivalent if you don't use SQL Server). Entity framework doesn't like dynamic result sets.

Stored Proc in RIA Services

I want to load some data with a SP.
I've put a SP in a Linq to SQL Class and I don't know how to use it for loading it's data in a datagrid.
In LinqToSqlDomainService I can't figure out how to call a SP.
What steps should I use.
Any samples of this ? All samples use a table.
Thank's
This post should hopefully be of help:
http://blogs.msdn.com/brada/archive/2009/08/24/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-part-24-stored-procedures.aspx
You can create empty view with the same structure of your sproc and map that stored procedure to your function in your DomainService
See sample on http://cid-289eaf995528b9fd.skydrive.live.com/self.aspx/Public/sproc.zip
I found the following excellent step-by-step guide at this site -
http://betaforums.silverlight.net/forums/p/218383/521023.aspx
1) Add a ADO Entity Data Model to your Web project; Select generate from database option; Select your Database instance to connect to.
2) Choose your DB object to import to the Model. You can expand Table node to select any table you want to import to the Model. Expand Stored Procedure node to select your Stored Precedure as well. Click Finish to finish the import.
3) Right click the DB model designer to select Add/Function Import. Give the function a name (same name as your SP would be fine) and select the Stored Procedure you want to map. If your SP returns only one field, you can map the return result to a collection of scalars. If your SP returns more than one field, you could either map the return result to a collection or Entity (if all the field are from a single table) or a collection of Complex types.
If you want to use Complex type, you can click Get Column button to get all the columns for your SP. Then click Create new Complex type button to create this Complex type.
4) Add a Domain Service class to the Web project. Select the DataModel you just created as the DataContext of this Service. Select all the entitis you want expose to the client. The service functions should be generated for those entities.
5) You may not see the Complex type in the Entity list. You have to manully add a query function for your SP in your Service:
Say your SP is called SP1, the Complex type you generated is called SP1_Result.
Add the following code in your Domain Service class:
public IQueryable<SP1_Result> SP1()
{
return this.ObjectContext.SP1().AsQueryable();
}
Now you can compile your project. You might get an error like this: "SP1_Result does not have a Key" (if you not on RIA service SP1 beta). If you do, you need to do the following in the service metadata file:
Added a SP1_Result metadata class and tagged the Key field:
[MetadataTypeAttribute(typeof(SP1_Result.SP1_ResultMetadata))]
public partial class SP1_Result
{
internal sealed class SP1_ResultMetadata
{
[Key]
public int MyId; // Change MyId to the ID field of your SP_Result
}
}
6) Compile your solution. Now you have SP1_Result exposed to the client. Check the generated file, you should see SP1_Result is generated as an Entity class. Now you can access DomainContext.SP1Query and DomainContext.SP1_Results in your Silverlight code. You can treat it as you do with any other Entity(the entity mapped to a table) class.
Calling a stored procedure is trivial. Import it as a function and then invoke the function as a member of the DDS. The return value is an ObservableCollection<> that you can use to set up the DataContext of the object you want to bind.
...unless you want to use it in a Silverlight RIA app via the magic code generated proxy, in which case your goose is cooked unless your result rows exactly match one of the entities. If you can meet that criterion, edit the DomainService class and surface a method that wraps the ObjectContext method.