MVC with EntityFramework model as stored procedures - c#

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.

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.

Scaffolding calling SQL stored procedure in model using ADO.Net

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

Entity Framework and SQL Server temp tables

We have a legacy application which we are moving to C#, and I would like to use Entity Framework (EF) for data access. SQL Server 2012 is the database, and currently I am using EF 5.0. The original application did a lot of data intensive processing. As part of that processing, the original application made extensive use of “temp” tables. Most of these “temp” tables were not actually SQL Server temp tables (e.g. #sometable in tempdb), but real tables created and destroyed in their own “temp” database in SQL Server.
Now, that I am working with C# EF I want to be able to use temp tables (of either type) as part of my data processing. I have done some googling on using temp tables with EF - and so far I have found that you can create the tempdb temp tables using the SQLBulkCopy, but there is no way to query against them using EF Linq, as they are not part of the normal data structure. The other option I have read about - is using a stored procedure to do the processing and passing it a table valued parameter. This would force us to have thousands of stored procedures - and put a great deal of the business logic in sprocs.
Is there any other way to create and use temporary SQL Server tables? Does EF 6 have any added capabilities in this area?
I've never seen temp tables used in EF.
If the data process in question is that intense, it's probably better to leave it as a stored procedure (I'm assuming this is how the legacy code worked). EF can run stored procedures with no problem; you can even get the results back as a model entity.
If you really don't want to use a stored procedure, you could also simulate a temp table in EF by using a regular table and just filtering it to the current session (through the use of a GUID or some other throw-away synthetic key). This technique works fairly well, but has the disadvantage of needing to clean the garbage data out of the table when your procedure is done.

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.

ADO.NET Data Services

I'm looking at using ADO.NET Data Services with silverlight. I've been looking at the
Data Model and Data Service Implementations (ADO.NET Data Services/Silverlight) topic on msdn. In their example, they build a data model generated from a database, and query entities in the datamodel. In my case I want to use stored procedures, so I"ve created the data model and added stored procedures instead of tables & views, but now I'm not sure how to execute them. Does anyone have a sample?
Just specify the stored procedures when you build your Entity Framework model. There's lots of documentation about this. You can also see a screencast.
As a point of clarification, I don't think there is anything Data-Services specific about your question. It's more of an Entity Framework question.

Categories

Resources