I have a ASP.NET MVC5 project with EF6. In SQL server 2008 I have a stored procedure that takes 2 parameters, an ID and a language ID, and returns data based on the parameters.
I want to call this stored procedure in one of my controllers to view the data. But I don't know how to do this. Can anybody help me or provide a tutorial for it?
I have the stored procedure in the project as an ADO.NET entity object already, it has a model class and such thus.
You can use Entity Framework's ExecuteSqlCommand to achieve this:
SqlParameter param1 = new SqlParameter("#param1", "Item1");
SqlParameter param2 = new SqlParameter("#param2", "Item2");
context.Database.ExecuteSqlCommand(
"sp_StoredProcedureName #param1, #param2",
param1,
param2);
Related
I have an Oracle database which contains some tables with no foreign or primary keys on them. I will create some stored procedures to handle the interaction with these tables, takes some parameter and return some values.
I would like to create an asp.net MVC app using EF and deal with this database using only the stored procedures. I don't want to bring any of the tables to the app as entities or anything.
I have been googling this, but all I can find is examples where the tables(entities) themselves are in the EDM and the stored procedures are being added to them for the interaction.
EX:
Is it possible to get the EF to deal with stored procedures only and provide me with models based on these stored procedures? if yes would you please direct me to the right materials?
Thanks,
You can use ExecuteStoreQueryList or equivalent on dbContext directly, create dto model result returned.
A sample:
using (var ctx = DataContext())
{
return ctx.ExecuteStoreQueryList<MyModel>(
"EXEC dbo.[xxx] #xxx, #xxx, #xxx, #poleID, #xxx",
new SqlParameter("xxx", xxx),
new SqlParameter("xxx", xxx),
new SqlParameter("xxx", (object)xxx?? DBNull.Value),
new SqlParameter("xxx", (object)xxx?? DBNull.Value),
new SqlParameter("xxx", (object)xxx?? DBNull.Value)
);
}
This question already has answers here:
using stored procedure in entity framework
(6 answers)
Closed 6 years ago.
I am writing a program which gets data from a database using an entity datamodel (database-first approach) with a stored procedure. I am a beginner to ASP.NET MVC, can anyone answer....
I bind database tables and stored procedure also,but how can i bind stored procedure to controller method to insert data(database first approach am using)
If you are using EF db-first and you want to call your stored procedure from controller's action, first of all, you should create a EF dataContext instance inside your action:
public ActionResult MyAction(){
using(var ctx = new MyDbEntities()){
ctx.MyMappedProcedure(arg1, arg2);
}
//todo: manipulate your data and/or return result from controller
}
Since this question created a lot of confusion let me put in this way :
I am working on an MVC C# project with Entity Framework. I am using a web service to implement a billing feature. Here I am passing values like this:
Billing.cost bills = new Billing.cost();
bills.Charge(0,"model",8,0,"Date",0,1,1,1,0,0,"N","APP","myemail",1,"cost");
Here in place of a model I need to pass my stored procedure name which has required values. My complete code with Stored procedure :
using (var ctx = new market_Entities())
{
ctx.uspmodel(10);
ctx.SaveChanges();
Billing.cost bills = new Billing.cost();
bills.Charge(0,"ctx",8,0,"Date",0,1,1,1,0,0,"N","APP","myemail",1,"cost");
}
Here 10 is an ID which i am passing as an input parameter. In charge method, 2nd parameter that is ctx is of string type and it is reference to my SP.
Problem I have is :
1) My stored procedure doesn't have output parameter.It is taking id as input parameter and based on that it is selecting name, value and address. So in place of ctx, I need to pass address result which I am getting from my stored procedure. But how can I select only particular property from market_entities and pass it as parameter ?
2) when I put breakpoints and check for results, I am not able to see any results.So how can i check results for a stored procedure in MVC which doesn't have output parameters?
New to Entity Framework .Using EF4. I have found articles and managed to use stored procedures to return a list of entities.
But cannot see/work out how you return a single entity.
Given that I have a stored procedure "GetCustomerById" that return a single customer How do I map it?
Using the Model Browser I right click on "Function Import" and I have added my StoredProcedure
however whatever I select does not seem to return a "Single Entity"
Am I missing the obvious?
thanks a lot for any link or suggestions
When you do Function Import you need to select the entity your SP returns from the drop down (i.e. Customer). The catch is EF does NOT directly returns Customer object as per your selection but System.Data.Objects.ObjectResult which implements IEnumerable.
To be more specific, here is the generated code for your function:
public ObjectResult<Customer> GetCustomerById(Nullable<global::System.Int32> Id)
That's because EF has no idea if your SP returns a single record or a list of them so it wraps the result inside ObjectResult. You can enumerate through this to get your Customer entity like any other IEnumerable object. For example:
Customer myCustomer = context.GetCustomerById(1).First();
I have just started a new web application with asp.net mvc and sql server 2005... Thus far in my webform applications i used ADO.NET... I want the same in my asp.net MVC apllication...
I have a database in sql server 2005 with all the stored procedures... Now i want to use those stored procedures in my MVC application... I dont use any ORM... I want to use ADO.NET..
Any sample controller that calls a model which uses a stored procedure and returns a Dataset to the controller and then the controller to View to display records... Any suggestions...
I would recommenced using a service or a repository that is responsible for populating the model with the data from the stored procedure that the controller sends to the view. I'm not sure on the reasoning for avoiding ORMs with the desire to use ADO.NET. The reality is entity framework, Linq2Sql, SubSoncic, and NHibernate are very proven and reliable.
Here is a quick sample I put together... It uses the SqlConnection and SqlCommand to call the stored procedure... Put this code in a separate class that the controller calls to get the Model.
public BlogEntry GetBlogEntry(int blogId)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = MyConnectionString;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandText = "mySproc";
sqlCommand.Connection = sqlConnection;
sqlConnection.Open();
var reader = sqlCommand.ExecuteReader();.
var blogEntry = new BlogEntry();
while (reader.Read())
{
//do something to fill your model...
}
return blogEntry;
}