I'm in the process of refactoring a project that includes the use of EF. Right now there is an ASP.NET MVC ViewModel that has multiple lists. An example would be to query an populate all the information about a customer from a customer table, a list of all their orders, another list of returns, and maybe another list of "Wish List" items.
The way it's setup now each one of those Lists are populated from an EF call which is effectively a database call. So to populate that ViewModel it makes 4 calls to the database.
From my research, all the examples are of either joining multiple tables into one record set or the use of stored procedures to return multiple record sets.
In my scenario it's much more difficult to create an Oracle stored procedure than to just use EF to populate the Lists so I would prefer not to use stored procedures.
Are there any suggestions or examples on how I can populate multiple lists that have different queries using one EF call without a stored procedure? I'm trying to reduce the number of database calls.
Related
I have a grid view with complex filtering options, the query is built with
an IQueryable on my dbcontext. This is working fine, the table can contain a lot of data, but the result is filtered with sane paging options.
I now have a requirement to implement an export feature that must work with all the filtering options available for the grid.
Let's say the table might contain a million rows.
I think I will run in performance issues by executing this with EF.
I also could create a stored procedure, but that would be quite complex and the logic will kind of be duplicated from the c# code.
Would it be a good idea to instead use the existing logic that build the IQueryable and generate the query string using
((System.Data.Objects.ObjectQuery)myIqueryable).ToTraceString()
Then I can run the generated query with ExecuteStoreQuery (or any other way to run sql directly)?
I tested this and it seem to work fine, but I am not sure how the performance vs a stored procedure would be or if I will run into issues I did not think of.
I have an Entity Framework DbContext with two different DbSets.
In my view I am combining these two sets into the same view model and listing them in the same table.
I want to support table paging to be able to query only a page of records at a time sorted by a particular column. I can't see how to do this without reading all of the records from the database and then paging from memory.
For example, I want to be able to sort by date ascending since both tables have a date column. I could simply take the page size from both tables and then sort in memory but the problem comes into play when I am skipping records. I do not know how many to skip in each table since it depends on how many records are found in the other table.
Is there a way to manipulate Entity Framework to do this?
It is possible.
JOin them in the database (can be done in EF).
Project that (select new {}) into the final object
Order by, skip, take on that projection.
It will be crap performance wise but there is no way around that given you have a broken database model. It basically has to get a tempoary view of all rows for the SQL to find the first ones - that will be slow.
Your best bet is going to be to combine them with a stored procedure or view, and then map that sp/view into Entity Framework. Combing them on the client is going to kill performance - let the server do it for you; it is clearly a server side task.
I am working on a project using entity framework code first approach, I have a situation where I need to call a stored procedure which returns multiple table, hence I want to map the result to my model. please tell me if its possible to do it and if yes then how can i do it.
Code First currently only supports mapping to tables. This unfortunately means
that you can’t map Code First directly to stored procedures, views, or other database
objects. If you are letting Code First generate a database, there is no way to
create these artifacts in the database, other than manually adding them once Code
First has created the database. If you are mapping to an existing database, there
are some techniques you can use to get data from non-table database artifacts.
i am also facing the same problem and not able to get any solution, so i called stored procedure using ExecuteReader and then mapped it to models using autoMapper.
Let me know if you are looking for code
I have a Stored Procedure that returns a dynamic result set based on a temporary table. My project uses LINQ for Data Access, but I can't incorporate LINQ with this Stored Procedure because it has a dynamic "shape" (I can't say before hand which columns will come back or how many there will be), so LINQ can't generate at design time an object that can hold the results.
I am trying to integrate the stored procedure the old fashioned was, using SQLDataAdapter and such, but I was hoping that I could still tie into LINQ so that I don't need to manage a separate transaction mechanism as that would be a disaster.
Is this possible? Thanks.
One best practice is that a stored procedure should never be coded such that it gives different columns in a result set based on inputs.
It sounds like your design is causing the single stored proc to try and do too many things. I'd highly suggest you change this.
First - you aren't using "LINQ" for data access. My guess is you are using Entity Framework for data access. If you are, it depends on which version of EF you are using.
New in Entity Framework 4 is shaping data from a stored proc. For step-by-step instructions, see this blog post: http://blogs.msdn.com/b/nihitk/archive/2010/04/23/ado-net-entity-designer-in-vs-2010-stored-procedure-return-type-shape-sensing.aspx
If you are using Entity Framework 1, which comes with .NET 3.5, then no EF does not have the ability to sense the shape of the stored proc data.
I have stored procedures that return couple of tables, and i want to map objects to the tables. Until now, i worked with type data set, and i want to stop working with them. I am looking for suggestions on how to do that, i thought about reflection, or iterate through each table in the data set and populate my object, or split the procedure to couple of procedures that each one of them return one table and iterate those tables with dataReader?
** Each object represent table in the data base.
** Each table in the procedure result contain data from specific table.
Thanks..
There are a whole bunch of options for connecting database tables with C# code, as described in this recent question.
In short - no need to do all of this manually - though if you have existing classes you need to integrate, using manual mapping might be pragmatic this time around.