How do I return dynamic column stored procedure results with ASP.NET Web Api OData provider? - asp.net-web-api

I'm banging my head against the wall with this one.
I'm trying to build an ASP.NET Web API OData service for a customer. The data returned comes from a SQL Server stored procedure that returns data with different structure every time (the columns may change on every execution).
I tried to do it with an entities model but of course - the whole point of EF is a pre-known data model and I found it impossible to get it to work.
I also tried using ADO.NET - get the stored procedure's data using a SqlDataReader and building a DataTable object with the columns (that change every time the stored procedure is executed) but I keep getting configuration errors, http errors and it looks like I'm not getting it right. Getting the stored procedure data worked but something with getting the data to be returned to the OData client doesn't work properly.
Google for several hours and tried several approaches but nothing works. I'm using Visual Studio 2013 (maybe it's too old a version?)
Did anyone succeed in getting variable structured data to be returned from a Web Api OData controller? Any sample projects that show how to do it?
My client is Excel (importing from an OData feed).

Related

Combining metadata from multiple sources

In a SPA app using breeze, how would I go about combining metadata from multiple sources for related data so that I can use them in 1 manager on the client. For example, I might have the following
Entity Framework Metadata from WebAPI controller (e.g. Account)
Custom Metadata from DTOs (e.g. Invoices)
Data from a third party service with metadata provided from client side metadata (e.g. Invoice transmission result)
In each case the data has related properties so I might want to be able to use Account.Transactions.TransmissionResults
UPDATE
I have tried several ways of getting this to work but to no avail. From Jay's answer, it is not possible at present to update the metadata from the server once it has been retrieved, so if and until that changes (see breeze user voice issue) I am left with one of the following approaches
1 Retrieve metadata from the server from Entity Framework and add metadata on the client to add extra entities. This worked to a degree but I could not add navigation properties from entity types added on the client to entity types retrieved from the server because I cannot add the foreign key association to the entity retrieved from the server, again back to the need to modifying metadata after it has been retrieved.
2 Write the complete metadata by hand, which will work but makes maintainability that much harder and seems wrong to be manually writing mostly the same code that the designer would write.
3 Generate most of the code from Entity Framework as described in the docs and then update it afterwards to add in the custom entities. Again similar issues than with option 2, it seems hacky.
Anyone else tried something similar? Is there something I am missing, which I could be, I've only started with breeze and js.
Thanks
A breeze EntityManager can have metadata from any number of DataService endpoints, and you can manually add metadata (new EntityTypes) on the client at any point. The only current restriction is that once you have metadata from a specific service, you can't change it. ( We are considering reviewing the last restriction).
So the question is, what are you trying to do that you can't right now?

Sample of how to create and save new entity, Breezejs

Im trying to create a very basic Todo-entity from the Breeze example.. Im doing:
manager.createEntity("Todo",{Description:"Test",Id:32,IsDone:false});
But I keep getting an error where the console tells me that it couldnt find the Type "Todo".. I have tried all types I can possibly think of..
Could anyone please provide me with an example of how to create an entity for the breeze WebApiSample.. cause this is driving me nuts..
Thanks alot!
Breeze needs metadata in order to create an entity of any type. That metadata can either be returned from the server or created locally. My guess is that you haven't yet done either of these. Metadata from the server is usually automatically fetched by Breeze during its first query against the server. Alternatively you can cause it to be fetched directly via the MetadataStore.fetchMetadata method.
Hope this helps.

Web Api and Odata without Entity Framework

I am having trouble getting OData to work with Asp.Net Web Api when the underlying data is NOT coming from Entity Framework.
I am using the latest OData Nuget package (Microsoft ASP.NET Web API OData 0.2.0-alpha release) but when I attempt to pass an OData query (say $top=10) I receive the error:
The given key was not present in the dictionary
If I don't send an OData query I can call the method just fine. The other methods in the same Web Api project that use Entity Framework work fine with OData queries. The one's that don't work are using Subsonic ORM to query an underlying AS400 data source. It returns an IQueryable. This worked just fine before the VS 2012 and .NET 4.5 RTM was released and OData was moved into a separate package. (i.e. worked with beta and RC versions of VS2012 and .NET 4.5)
Any ideas would be appreciated.
I guess this issue is caused by stable ordering, which doesn't work well with the underlying query provider. Could you put the call stack of the error to confirm it?
One big change with OData query composition in this release is that it ensures stable ordering before taking top items. The reason to do that is user may have random data source which makes return data keep changing.
The way web api odata package does is to add OrderyBy [Keys] query before executing top. Or if there is no key defined in the model (Keys are ID, EntityID, or [Key] attributed property), it will use all the primitive properties in the model to order.
If you can make sure that the data source will always return data with stable ordering, you can turn off this feature by code:
[Queryable(EnsureStableOrdering = false)]

InnerException {"Invalid object name 'dbo.Users'"}

I am currently trying to setup an ASP.Net MVC 3 Application for the first time. I know what I am doing with Ruby on Rails but completely lost with ASP.Net. I would like the app to create the database but not sure what I am doing wrong. I have an empty Database build called ssDB. I have setup the connectionStrings and built out the Model classes. According to everything I have read thus for, when I go to the run the App it should create the database but it doesn't look that way.
The Abbreviated Error I get is:
InnerException: System.Data.SqlClient.SqlException
Message=Invalid object name 'dbo.Users'.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=208
Procedure=""
Server=TESTSVR01
State=1
I have an empty Database build called ssDB
You have created an empty database with no tables. Since there is a database EF assumes tables are also created. Hence it will not proceed to create the tables.
You can drop the database and allow EF to create the database and the tables.
I also experienced the same issue while using Database first approach. Connection string in my service's/service's host web.config was incorrect. I corrected the connection string and it worked.

How to write a datatable to excel work book at client side

Currently i am doing one project in that we need to generate report from database.
Since my server memory is too low im getting 'Out of Memory' Exception when im writing it at serverside and also when i write directly to a excel file using http header as excel file im not able to create multiple sheets since my database table is huge more than 65536 rows.
I saw many solution using a third party tool but i cant use those into mine..If anyone already worked on this please give me some direction.
Also i tried using javascript but for that i need to use datagrid at server side??
but in my project i m not allowed to use like this.
You can open an excel file, as an xml document using the Open XML format SDK: http://msdn.microsoft.com/en-us/library/bb448854.aspx

Resources