Stored Procedure without mapping with DbSet - entity-framework

Can we call stored procedure without mapping with DbSet using Entity Framework Code First approach
Thanks

You can use raw SQL and call the procedure that way.
var blogs = context.Blogs.SqlQuery("dbo.GetBlogs").ToList();
or
var blogs = context.ExecuteSqlCommand("dbo.GetBlogs").ToList();

Related

How to user "merge" in entity framework code first to merge the multiple result set from a stored procedure into one single collection

I am new to entity framework. I have a simple stored procedure that returns 2 result set.
I am using a code-first approach and need to merge the 2 result set into one single collection using "merge". I don't want to modify my stored procedure.
Any help would be appreciated. Thank you.
Just follow the doc here:
Entity Framework Sprocs with Multiple Result Sets
Except in your case both DataReaders load the same entity.
eg
var foos1= ((IObjectContextAdapter)db)
.ObjectContext
.Translate<Foo>(reader, "Foos", MergeOption.AppendOnly);
// Move to second result set and read next set of entities
reader.NextResult();
var foos2 = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<Foo>(reader, "Foos", MergeOption.AppendOnly);
var foos = foos1.Union(foos2).ToList();

Updateing a Table with a Stored Procedure or Functions in Catel

I've set up a database with EF6 and Code First and I am using Catel's Repository Pattern to communicate with the database.
Now, I am curious as to how you would call a stored procedure (or even a Scalar Valued Function) on my database using Catel. Any suggestions?
You should still be able to call the stored procedure using the DbContext itself as you usually would:
using (var dbContextManager = DbContextManager<MyEntities>.GetManager())
{
var dbContext = dbContextManager.DbContext;
var result = dbContext.Database.SqlQuery<ReturnType>("storedProcedureName", params);
}
https://msdn.microsoft.com/en-us/library/gg696545(v=vs.113).aspx

stored procedure with EF 4.1

I have a parameterized stored procedure which is executing a view and return the results. The view is showing results of join of two tables. I need to pass parameters to this stored procedure and call it from MVC3 controller action using EF 4.1 code first approach and return the results. How can I do this. Please suggest step by step.
Thanks.
I don't know what part of "Code First doesn't support stored procedures" people don't understand, but this keeps coming up and it shouldn't. You can muddle your way around it (kind of), but really if you need to execute stored procedures then you shouldn't be using code first at this point.
You might check this post as well. It might help with at least the first step: executing a proc with parameters in EF 4.1
How to use DbContext.Database.SqlQuery<TElement>(sql, params) with stored procedure? EF Code First CTP5
My Code:
context.Database.SqlQuery<EntityType>(
"EXEC ProcName #param1, #param2",
new SqlParameter("param1", param1),
new SqlParameter("param2", param2));
I'm not sure if this will help you but here's how I call an Oracle proc using EF 4.1 and the DevArt driver. This just returns a single value but you may be able to adapt it to return multiple rows. I have a package.proc called P_SID.SID_PGet:
PROCEDURE SID_PGet(io_SID OUT varchar2) is
Begin
io_SID:=GetSID;
End;
Below is how I call it and retrieve the SID value:
var parameter = new Devart.Data.Oracle.OracleParameter("io_SID", Devart.Data.Oracle.OracleDbType.VarChar, ParameterDirection.Output);
this.Database.ExecuteSqlCommand("BEGIN P_SID.SID_PGet(:io_SID); END;", parameter);
var sid = parameter.Value as string;
return sid;

ADO.NET Ef4- How can i map a entity to stored procedure without mapping to table

We are using ado.net entity framework 4.0 for our database layer and I am a newbie to ado.net entity framework. I have created entity via adding a entity in entity framework. I want to map that entity with stored procedure only not table of the database. Stored procedure will return same column as entity. How it is possible and how i can do that without mapping to table?
Here is a complete walkthrough http://msdn.microsoft.com/en-us/library/cc716679.aspx
Its not possible because an ObjectSet is an IQueryable and mapping an ObjectSet to stored procedure would not give u an IQueryable because stored procedures by their very nature cannot be composed. The best you can do is take the content inside the stored procedure and put into a view and map the view to an ObjectSet which is possible.
You need to create a complex type, not an entity. Open up the model browser and import your stored procedure as a "function import" (your SP must not use #tempTables but you can use #tableVariables instead); in the function import wizard you'll see a "create complex type" button.
The SP becomes a method in the model context and you can use it to get IEnumerable[TheComplexType].
In EF4.1 code-first it's even simpler, you put a [ComplexType] attribute on top of any class and you can use that type as a return type for context.ExecuteStoreQuery[T]. If your properties are named exactly as the returned columns are (and the types line up), the mapping is "magic" - it just works.

Entity framework function import, can't load relations for functions that return entity types

I've created a function import that returns the results of a stored proceedure as one of my entities. however I can't seem to traverse my through navigation properties to access the data in other entities. I know that you can use include() for objectQueries but can't find anything that will force the EF to load my relations for entity results of function imports.
Any ideas??
Thanks in advance.
This is not possible in EF 1.0
The reason is that EF will consider stored procedure values to be just values and not navigation properites.
For example, Employee entity has multiple Order entities. In Order you have a property called EmployeeID. When the database fills your query using include statements, it creates 1 projection query in SQL to populate all of the Order data that a particular Employee could have.
So if I said
var employee = context.Employees.Include("Orders").Where(e => e.ID == 1).First();
var orders = employee.Orders;
The SQL for the first query will create a projection query which will contain orders where the EmployeeID = 1.
Now when your stored procedure runs, this can do any code behind the scenes (in otherwords it can return any set of data). So when SQL runs the stored procedure, it just runs the code in that stored procedure and does not have any knowledge that EmployeeID on Order is an FK to that property. Additionally, if your stored procedure returns an Employee entity, then you are looking at another scenario where you will not even have an OrderID to pursue.
To work around this though, you can setup your query in EF using Include statements that can mirror any stored procedure. If you use the proper mix of .Select and .Include statements you should be able to do the same thing.