Scaffolding calling SQL stored procedure in model using ADO.Net - c#

I am new to ASP.NET (using MVC5 EF 6.1.1) and just developed a new prototype for an app to use at work. I am currently calling my stored procedures in my controllers and was asked to redesign using a Database First approach before my app can go live. I was recommended the following tutorial:
http://www.asp.net/mvc/overview/getting-started/database-first-development/creating-the-web-application
This works great if you select one or more tables from your database on the Entity Data Model Wizard (scaffolding works perfectly). However, I need to select a stored procedure instead of a table and scaffolding won't work. I believe I need to create a partial class in my Model folder to somehow call my stored procedure with its arguments (i.e. var result=dbContext.Procedurename( values1, value2)) but I haven't found a proper example of this and have been blocked at this stage for over a month. Could anyone help?
Thanks!

Look for SqlQuery or ExecuteSqlCommand
context.Database.SqlQuery<Entity>("spName #param1, #param2, #param3");
context.Database.ExecuteSqlCommand("spName #param1, #param1,#param1");
Better solution:
Using the wizard to create data model with ADO.Net Entity Framework, along with selecting tables, add stored procedure as well.
Doing this will allow us to use context object to execute store procedures in controller.
ContosoUniversityEntities context=new ContosoUniversityEntities();
context.mysp();
For your consideration: calling SP in EF

Related

.Net MVC Database first model with stored procedures

I'm developing a MVC5 web project in VS 2013 and I have to use an already existing database and its Stored Procedures so I'm looking forward to using Entity Framework database first approach to help me model the classes.
My question is, should I create the classes (the model) directly from the tables using EF? i mean should my classes represent a table in the database exactly the way they are? - given that some stored procedures return a combination of different attributes from different tables, I'm confused as what the classes on the code should represent exactly.
Also i want to have my own form to let users upload and read their info, so scaffolding the views to create the read/update/delete won't come handy for this task, will it?
Thanks!
If it is code first then you can use the EF tools to scaffold your database for you from your existing database. If it's database first, all of the database models are generate for you anyway and whenever you update your database the models can be updated to reflect the changes for you.
If you are using stored procedures for code first, you'll need to create objects for each stored procedure so that the return values can be mapped back to an object. These should really match precisely the data that is being returned back in both type and naming:
this.Database.SqlQuery<YourEntityType>("storedProcedureName",params);
As for having your views scaffolded for you, I think you should take one step at a time and see what works for your use case or not.

MVC with EntityFramework model as stored procedures

We have an Asp.net Web application with a normal Ado.NET Oracle connectivity.
The back end works completely on stored procedures.
Now, our management has asked us to upgrade the entire application to MVC.
The management has come up with some standards where they say we must use EntityFramework model and go with the same set of stored procedures without any change.
Here is my question. Each action in my application is running from stored procedures written in the Oracle DB. Is it possible for me to call the exact same stored procedures from Entity framework in Oracle. How can I achieve this?
The stored procedures does plenty of things in the back end like insert,update, select or all in most of the cases.
I know entity framework needs an Entity model. If stored-procedures
will work, what will be the entity model. Is there any workaround for
this?
Try out this example for using stored procedure with Entity Framework, maybe you need to understand the things in your project, whether to use entities or complex types as part of your stored procedures.

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();

Mapping select stored procedures in entity framework

My scenario
I'm using Visual Studio 2010 with Entity Framework 4.1
I have a legacy database with many tables and many stored procedures.
I'm writing an ASP.NET C# program using MVC 3
I have adopted the 'database first' design using ADO.NET DbContext so I have an edmx with all the models and associations and navigation properties nicely set up.
I can map the insert, update, delete procedures to the relevant models.
I've used 'Function Import' to import other stored procedures.
However, I can't find a way to map my Select procedures to select actions (select by id, select list, select by filter etc).
EF seems to use lazy loading so what I want to happen is when an object fetches its child objects it uses the stored procedures already written.
(The select procedures take into account an 'IsDeleted' flag, and use the 'ORDER BY' clause, amongst others)
I see from this article
http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx
that Linq to SQL allows drag and drop of SPs, which sounds, more or less, exactly what I want.
I've also come across the term DefiningQuery.
http://msdn.microsoft.com/en-us/library/cc982038.aspx
Is this what I want? I don't like the note 'Any changes made to the storage model, including defining queries, will be overwritten when you run the Update Model Wizard.'
In summary, what I want to happen is when an object fetches its child objects it uses my stored procedures.
Can I achieve my goal using Entity Framework?
Have I missed something obvious?
Or should I try to be really clever and modify the db Entity T4 template, so that, for example, my generated Address model has this property:
public virtual ICollection<AddressLine> AddressLines {
get{
DBWrapper _db = new DBWrapper();
return _db.GetAddressLines(AddressID);
}
set{};
}
where GetAddressLines is custom function that calls a function import and does the neccessary conversions.
It is not possible. You can import your stored procedures as function imports and manually execute them but you cannot replace queries generated by EF with custom stored procedures.
Except that you can, sort of.
Take your most basic select stored procedure (i.e., the one which is closest to "select * from mytable", and use it to define a view in your database. Have entity framework use this "myview" instead of "mytable". Then map your insert, update and delete stored procs for this view-based entity as you did originally for your table.
Finally, use function imports for your more selective selects, and define them to return collections of your entity. So if you had something like a Person entity, and you had a stored proc called something like FetchPersonByAge(int), your entity would end up with a static method called something like "GetByAge(int)". You could then call it in code like this: var people33 = Person.getByAge(33);
I have done this, and it worked quite well, allowing me to respect a legacy database's designers demand that all database access be through their stored procs, and no user code directly accessing tables. See Julie Lerman's article:
http://msdn.microsoft.com/en-us/data/gg699321.aspx
Dave

Querying Entity Data Model from C# code

I have an application with an Entity Data Model. I do not completely understand the value of an Entity Data Model. Either way, I know that it connects to my database and interacts with it. I have some C# code from which I want to execute a stored procedure. My problem is, I'm not sure how to do this using the "Entity Data Model" approach.
I'm familiar with the System.Data.SqlClient namespace, but that approach does not seem applicable here. Can someone tell me how to interact with an Entity Data Model sproc via code?
Thanks!
Here's a great video tutorial on "Practical Entity Framework for C#"
You can call a stored procedure on SQL Server like this:
myEntities.CalculateCustomerInvoices();
Here's an article on "Using Stored Procedures for Insert, Update & Delete in an Entity Data Model"
This MSDN page explains how to work with stored procedures with the entity framework: http://msdn.microsoft.com/en-us/library/bb896279.aspx.

Categories

Resources