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.
Related
I'm currently trying to read data from a table out of SQL server. The table has 10 columns and when i'm reading the base table by itself, everything works out just fine.
The trouble is, there are X number of extra property tables that may or may not go with my base table. Some databases only have the base table with 10 columns, while others have property tables containing more columns that must be joined into the base table in order to properly display the needed data. Is there a way via EF6 to load that data into a queryable source in a decoupled way?
Basically due to the fact that the extra tables are constantly in flux, I cannot rely on generating models for them and using the mapping EF provides. I do have a model for the base table as its 10 columns never change. I also have a mechanism to read the relational information in order to get the names of the property tables and columns that my program needs to display with the base table when they are available.
Any insight is greatly appreciated.
Good old-fashioned ADO.NET does a good job giving you run-time access to arbitrary query results. You can examine the column data types returned in a DataReader or after loaded into a DataTable, and access columns by name or ordinal position.
And you can interop beteween EF (for your design-time models) and ADO.NET tables that vary at runtime.
I have multiple relational tables in the following format.
enter image description here
I'm trying to query that data in an efficient way in .Net so that I can perform transforms on the data (array to object) and insert into DocumentDb. Essentially doing some ETL work, but because the data has to be transformed a certain way and going into DocumentDb, we are using .Net.
We are inserting into one document collection data from all relational tables, so there will be lots of
// if still in same profile record, insert more relational data for each relational table.
We are trying to avoid cartesianing(sp?) the data so that one profile doesn't have 100 records or more. We were thinking about using some of the Oracle methods to convert child records to a Json Array, but can't upgrade the Oracle system to the release that allows for that feature. Another thought was to use create an xml document, but that feels pretty wrong.
Any ideas on essentially the best practice for handling ETL work within .Net? Most of the web sites I've worked on involve only pulling from a few tables at best and a lot are 1:1 relationships.
You could use EntityFramework for this. Simply create a DBContext, and all the POCO classes that represent your tables with their relationships. Then execute the necessary query on your DataSet and you'll have all the data mapped to your objects, which then you can serialize in any way you want.
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.
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
My scenario.... I dynamically(dicval.add(Tkey,Tvalue) create set of columns and now i need to insert values and save those values using Stored procedures..how to achieve this
not sure exactly what you are trying to accomplish, what your schemas are, etc.
w/i dynamic sql, one can run DDL statements - create table, alter table add col, etc
from what little I see in your answer, you may be
If you are using linq, perhaps it contains structures for building tables?
Again, from what little I see of your question, maybe the correct question to be asking is, "how can I change the structure of this app so that I'm not having to dynamically add columns"
As far as interacting with dynamically, recently created tables via stored procedures - that seems to say to me that the sprocs would need to be dynamically created as well.
If you can't change the structure, and insist on doing some sort of string manipulation to create DDL of tables, columns and sprocs, I highly recommend you be very, very organized and make use of tokenized templates.