How to call stored procedure with input parameters in ADO.NET Entity Framework 4 in ASP.NET 4.0 - stored-procedures

I am using Database first modal and EF4.0.
I have created a stored procedure which required two input parameters and return dynamically column in result-set i.e. columns are not fixed in result.It may be 5 or 7 or 10 and so on.
I am using Grid-view and passing the result-set in it's data-source.
How can i call stored procedure in this case.
thanks.

You must use ADO.NET and data reader / data adapter directly. EF can work only with stored procedures producing strongly typed results known at design time (you must create class for them).

Related

Fetching multiple records from multiple tables using stored procedure in Entity Framework in ASP.NET MVC 5

create procedure GetCityArea
as
begin
select * from CityMaster;
select * from Area;
End
This is my stored procedure GetCityArea which returns multiple record.
My exact problem is I am getting multiple record from the stored procedure, and I want to show this result in single view as separate tables. I am new in ASP.NET MVC and I want to solve this problem using Entity Framework. Please help.
You can do this using ObjectContext and the Translate method.
The gory details are here: https://msdn.microsoft.com/en-us/data/jj691402.aspx

Stored procedure in entity framework 4.3 + mvc 4

My application is using ASP.NET MVC 4 and using stored procedure in EF 4.3
I know, in EF if I am passing parent entity only then also I can add child entity with just in one line of code.
But I am confused how can I insert data in parent-child tables using single stored procedure?
What I want to is my parent's table is having primary key and which I want to insert in child table as foreign key. So how can I do this in one stored procedure using EF 4.3
You don't say what your database is or why you want to use EF4.3 in an mvc4 app or why you want to add entities using a stored procedure rather than just let EF do it for you taking care of the foreign key relationship as it does. You haven't said whether you're using a code first approach or using the entity model design tools
The reason why I bring this up is because ordinarily EF can handle creating parent child entities and will return the entities with populated key values by default and it's easier to code than using stored procedures. If you have to use stored procedures than EF6 has better support for stored procedures then EF4 did so if you must use stored procedures then can you upgrade to EF6?
If you are using code first with EF4 then your basic workflow would be to create a wrapper method for your parent entity and child entity each wrapper method accepts an entity as a parameter the wrapper for the parent entity then builds a database command from the entity with parameters populated with values from the passed in entity it then executes the command and the stored procedure command should return the key value of the new parent entity.
You can then update each of the child entities with the foreign key value and then pass each child entity to the respective wrapper method - which in turn needs to execute the relevant stored procedure which will also return the new primary key value for the child
You can then either just update your passed in entity with the new key values or use the key to get EF to fetch the new entity with the key - the latter is preferable if your stored procedure is doing more than just populating db fields ie calculating values etc.
I don't know what you would do or even if you can do this in the designer - I know that the designer in ef4 has better support for stored procedures than code first. But I would expect that if you're hoping to just pass the object graph to a stored procedures imported into the designer then this isn't going to work ( I don't know that you can't I would be surprised if you could) because you need to be able to pass in a collection of parameters for the child entities which in turn could have child entities.

How to access Stored procedure in Code First Model which return 6 or 7 table combine result

i am new in MVC and entity frame work and i want to create Code first entity frame work.
we have already created project in asp.net and we want to migrate in mvc. we have lots of stored procedure and some procedure return complex data combination of 10 to 12 tables...
As a Proof of Concept we wan't to develop 3 to 4 pages...
i have some question regarding new start.
1) Should i used entity frame work if yes then which is better entity frame work model
Database first
Model first
code first
2) how to integrate stored procedure in Code first model
3) in each page we have minimum 7 to 8 table result there... how i will handle in entity frame work.
this is my first project in mvc and entity framework please help me with appropriated answer.
first, this has nothing to do with MVC. this is purely a data access issue.
second, this is sort of missing the point of using entity framework. if the goal is to migrate away from stored procs that one thing, but to use EF and continue to execute the stored procs defeats the purpose of using a ORM like EF.
Instead for your procs I would stick with raw ADO.Net, or use Dapper.Net to convert the stored proc result sets into objects.
EF would be a better choice as your convert each proc into EF linq queries. It's not that you can't execute procs (or raw sql) from EF, but it doesn't make much sense. Especially with how you describe the procs.

How to connect Active Reports 7 to use a stored procedures?

How to connect Active Reports 7 to use a stored procedures? I have stored procedures already written, but how do you connect to them to use the data in the tables so that my report is dynamic.
ActiveReports 7 has two different report types, with slightly different ways of using stored procedures.
If you are using a SectionReport (i.e. the traditional form of report that was available in ActiveReports 6) then you can execute a stored procedure via the EXEC SQL command in your query. f.e.
EXEC sp_mysp '<%myparameter%>'
In a PageReport (i.e. the new report type introduced in ActiveReports 7) then you need to set the command type to Stored Procedure and then the query string is set to the name of your stored procedure. If you have any parameters to pass then you can pass them in via the DataSet's parameters page.
In PageReports when working with the ADO.NET based data providers (SQL Server, OLE DB, and Oracle connection types) there is a 1-to-1 correlation between objects in the data source, data set, and parameters in Page Reports and what you see in the related ADO.NET *Connection, *Command, and *Parameter classes. So if you have questions about how those work you can just look at how you would write the same code with the ADO.NET classes.

Property in Entity partial class

I have an entity/table that uses sqlgeography.
Since EF 4.X doesn't support spatial types I'm instead sending the bytes of the field back and forth.
I have stored procs on the database side that handles the converstion and properties on the code side to do that job.
To add the properties in the code I used a partial class.
One of those properties is for the SqlGeography which simply wraps around the byte[] property to handle getting and setting.
This property is hidden from EF using the NotMappedAttribute.
The other is the property exposing the byte[] itself and is decorated with the EdmScalarPropertyAttribute and DataMemberAttribute.
I then go to the EF model designer (*.edmx) to point the entity model at the Insert/Update/Delete stored procs.
It finds the stored procs alright and realises that they (when appropriate) take a VARBINARY parameter.
It also has a drop down allowing you to select a property on the entity class which maps to that parameter.
However this drop down doesn't list either of my properties. I don't care about the SqlGeography property since that is meant to be hidden from EF, however it is vital for me to be able to point it at the byte[] property, as that is where the data comes from.
I would very much like to avoid database triggers or wrapper classes and addiitonal fields to fudge this in to working.
I tried manually editing the .edmx file to include the byte[] property, but then it just complains it's unmapped.
Can anyone give me some insight in to how to get this to work? Or an alternative method of achiving the end result?
We could use a view to create the binary field for us, but this then involves manually creating a lot of the xml for the relationships within the data.
This pretty much voids the point of using EF which is to make life simple and easy.
For this project We'll just add a binary field to the table then have sprocs to handle the converstion on the server and a property in a partial entity class for exposing the geography type in the model.
Next project I doubt we'll be using EF. Dapper is so much more painless, even if theres a touch more code writing involved.
Here's the links for using views if anyone thinks it would be applicable to them:
http://thedatafarm.com/blog/data-access/yes-you-can-read-and-probably-write-spatial-data-with-entity-framework/
http://smehrozalam.wordpress.com/2009/08/12/entity-framework-creating-a-model-using-views-instead-of-tables/
In the end we created a computed column for each table that exposes the spatial data as bytes.
We then use stored procs for inserting and updating the spatial data.

Resources