English 中文(简体)
RIA services, EF and stored procs
原标题:

I have this problem for 2 months now

How can I use stored procedures whith RIA.

I was using LinqToSql and everithing works fine.

I ve made an class in designer and mapped it to a SP.

Now in EF I saw this ComplexTypes I ve imported son SP with result in ComplexTypes.

But in DomainDataSource are not appear.

OK.

But how can I use them ?

I need only to retreive some data.

I ve tried all solutions (List, IList, etc.) but I get

Unable to cast object of type System.Data.Metadata.Edm.ComplexType to type System.Data.Metadata.Edm.EntityType . BusinessApplication2

Any solutions please ?

问题回答

The problem with stored procedures is that the RIA services tools cannot "see" which columns the stored procedure will return.

Therefore, there is a lot of code that is missing that would otherwise be generated.

Not the answer you are looking for but the solution is to not use stored proceedures.

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.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签