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.
Related
I am newbie to C # ASP.Net I use Microsoft.SqlServer.Management.Smo that has connected to SQL Server 2012 but can not load data from SQL form. You can guide me to use SMO.dll
Thanks!
The easiest way I have found is to use an ORM named Dapper with one of its plugins Dapper.FastCRUD.
https://github.com/StackExchange/Dapper
https://github.com/MoonStorm/Dapper.FastCRUD
There are many different ORMs out there and Entity Framework comes with Visual Studio. They are all very similar in operation.
first you would create a model which directly maps to your table structure. If using Dapper alone you would create your query in a string or specify a stored procedure to execute. Depending on the type of operation the result would either be a bool or model. When executing the operation you would set a cast of the model for the ORM to recognize when sending data to the server and receiving. If you are using a plugin like FastCRUD, EasyCRUD or others the query would be automatically created for you and based upon the model and the type of CRUD operation would execute the appropriate query on the server.
This is oversimplified but there is good documentation o Dapper that will get you moving pretty quick. the documentation is lacking for FastCRUD IMO but EasyCRUD does have better documentation just not updated as often from what I can tell.
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.
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.
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
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.