Dynamic SQL based stored procedure call in Entity Framework - c#

For one of our recent projects, we created a stored procedure which generated SQL and executed it in the end. The purpose of the stored procedure was to create pivots based on dynamic columns.
When trying to access it using Entity Framework using the usual function import when I tried to access the stored procedure, it would return anything as it requires a dynamic type to store the retrieved data.
Which in our case was a dynamic query and linq was unable to get the returned columns. So to work around what I did was call the stored procedure in the traditional way i.e. creating a DataAdapter and SqlCommand object and SqlConnection object.
But what is the proper way of calling this kind of stored procedure using Entity Framework?
Thanks in advance.

Entity framework doesn't support dynamic result sets from stored procedures. It also doesn't support stored procedures using dynamic SQL because it cannot get static result set declaration from the procedure. So you must either ensure that your procedure will always return static type (same number of columns with same names) or you must use traditional ADO.NET to execute that procedure.

Following steps can be followed:
Store the dynamic part of SP inside a variable and the print that variable at end of the SP.
execute the SP and execute it with some data.
open the Messages tab in Result window.
copy the code that is written after (x row(s) affected);
paste that code in the SP and comment out everything else until variables declaration.
execute the new modified SP and add it to the entity framework. This time, entity framework will make a complex type which you want.
uncomment the previous commented code and delete the data that you copied from Messages tab and execute it again.
Follow the same process every time you add or remove columns from the SP.

Related

Call stored procedure in loop in wcf

I have a problem with calling WCF service from ajax.
I pass to WCF an array of objects, and for every object, I need a stored procedure to be called via ADO.NET.
The problem is when I call this procedure in the loop, the application starts to lag.
Can you tell me, how is possible to fix it ?
Is it a stored procedure you can modify? You might get significant performance benefits by modifying the stored procedure to use table-valued parameters. That way instead of calling the stored procedure once for every object, you're calling the stored procedure once and passing all of the objects.
The linked documentation is thorough, although you might want to google for other examples. The first time through using them you have to learn some new steps, but once you get through that it's a powerful tool for scenarios like this.
On the SQL server you create a new table-valued type. The syntax is very similar to defining a table. Then you modify your stored procedure to receive that parameter. Within the procedure you select from the parameter just like you do from a table.
In your application you create a DataTable which corresponds to the table you've defined and then populate it with the rows of data you want to send.
If you've ever seen anyone doing weird stuff to pass multiple values to a stored procedure (like using comma-delimited strings) this is the antidote.

How to pass a list of data to a stored procedure, using Entity Framework 6

I am trying to pass a list of data from C# to a SQL Server stored procedure, using Entity Framework Database First. There are lots of articles in SO and elsewhere about the problem, but it is not obvious which refer to passing variable data to the stored procedure and those returning variable data from the database. They also tend to focus on the code to call the stored procedure but I cannot get that far.
A user-defined table type and a stored procedure were created and tested in SSMS. The problem occurred in the C#. When updating the model (.edmx file), this warning is issued:
Warning 4 Error 6005: The function 'uspGetBusyPersonsTVP' has a parameter 'list' at parameter index 2 that has a data type 'table type' which is currently not supported for the target Entity Framework version. The function was excluded.
Examination in the Model Browser shows that the stored procedure has been created in the model, but only with the non-TVP parameters.
This MSDN site https://msdn.microsoft.com/en-us/data/hh859577.aspx says that table-valued parameters have been supported since EF 5.0, but the article only talks about returning data.
I have just finished updating and I am now using Visual Studio 2015 Community, .Net 4.6 and Entity Framework 6.1.3.
Does anyone know if a TVP can be passed to a stored procedure, or if it is ever likely to be supported?
The article you reference talks about table-valued functions (i.e. a user-defined function that returns a table), not about passing table-value parameters to a stored procedure. You're comparing apples and elephants.
Whether EF will ever support it I can't answer as I don't work for Microsoft. Whether it can be done, the answer appears to be "yes" based on How to pass table value parameters to stored procedure from .net code, however it appears you'll need to use raw ADO.NET to do it.

How to use stored procedures in Entity Framework 6-database first?

I have an already existing database and i generated my ".edmx" file from the database. After that i added the stored procedure via update model from database menu and in the model browser i can see my procedure name and complex type but i can't see the complex type in the "IntelliSense".
dbContext.Database.SqlQuery<mycomplextype>
IntelliSense doesn't show the complex type when i write thecode to call the stored procedure. I didn't see anything related to the stored procedure in the context class either.
What is the proper way to add and use stored procedure in entity framework database first? Can anyone point out what i am missing?
Thank you.
Edit:
dbcontext.storedprocedurename
I can't see my stored procedures name in this way but the edmx shows the procedure in the function imports section in model browser.
I got 6.1.3 EntityFramework version and I am using Database First approach.
To use my stored procedure i added it to the Model with the helper it is reference as a method and call it like so in query
Dbcontext.StoredProcedureName(prop1,prop2,..); Dbcontext.SaveChangesAsync();

multiple resultsets in stored procedures

I have a stored procedure that is a class in my project. I gain the data returned by following:
EMTS.SPs.PrcEMTSDataLookup(so).GetDataSet().Tables[0].Copy()
This is placed in a dataset. The issue is the stored procedure has several selects and each select needs to be placed in a separate grid.
I have tried IDataReader and IDataAdapter but have not had any luck pulling data from the stored procedure object. I have research microsoft for information on IDataAdapter and the example they give uses SqlDataAdapter which is not necessary for me since the stored procedure is an object in the project.
So how do I get the different result sets in the sp and put them in separate grids?

Full Data Binding to a stored procedure?

Is it possible to do data binding to a stored procedure, similar to a table or view ?
(i.e. including select, update, delete, insert)
for selecting, I'm currently executing the procedure with ExecuteReader(), read it into a DataTable and then bind the table to a grid. But now, how to write back changes from the datatable into the database? Is there a "simple", built-in method in the .NET framework?
No, it isn't possible. A stored procedure could produce the returned results in any way possible with T-SQL code and it is not possible to create an update by calling the same procedure as was used to read the data.
A more modern way to work with data access is to use an OR-Mapper such as entity framework. Depending on your type of application (web applications where the data is sent to the client and then posted back and interpreted as new objects are typically a bit harder) it might be possible to do a simple data binding and save the changes back to the DB.
It is not possible with Stored procedure, but something similar can be achieved with SqlCommandBuilder
Here is a full sample which describes how to do that http://support.microsoft.com/kb/307587

Categories

Resources