Results 1 to 7 of 7
Jun 2, 2011, 03:49 #1
- Join Date
- Jun 2009
- 0 Post(s)
- 0 Thread(s)
Mapping Data To Non-Table Entites In .NET
Hi, I've got a problem I've been going round and round on, and every time I think I have a solution I seem to come back to a core requirement not being fulfilled (I will detail this later).
What I'm trying to do, at a simplistic level, is to return a collection of objects from data supplied by a stored procedure. The object hierarchy is a follows
Shipment Object - this has a ShipmentNumber string property and a List<OrderHeader> collection
OrderHeader Object - this has OrderNumber double, OrderType string, ShipmentNumber string properties and a List<OrderLine> collection
OrderLine Object - this had OrderNumber double, OrderType string, OrderLine double, ItemNumber string properties
Shipment <-> OrderHeader is a 1 to many based on ShipmentNumber on either object
OrderNumber <-> OrderLine is a 1 to many based on OrderNumber and OrderType on either object
This object structure visualises how I need to view my data for a particular report (it's actually a lot more complex than this, but this is a simple example I am initially trying to get working) and bears no resemblence to my database table structure. I am trying to map data from a stored procedure into this structure so that I end up with a collection of shipments, which then have a collection of order headers within them and these have a collection of lines within them, but I don't seem to be able to do this. I can't seem to do it with LINQ so I am looking at the Entity Framework. It seems that you can set an SP to return several SELECT outputs and then use the ObjectContext.Translate<...>(...) method to map data into objects, like this, and then the Translate method will take care of applying the associations, but when I trying this I'm getting an error reporting that the entity object properties aren't mapped. Looking into this mapping it seems to come back to the objects having to refer to actual database tables (this is my recurring problem).
How, if it is possible, can I load this data, from a stored procedure(s), into this structure of objects using LINQ, EF, or something else without having to base my classes on database tables? Surely this is possible? I can do this with a single collection of objects, but the problem seems to come when these objects contain collections of other objects.
Just a few background points:- I have to use stored procedures to get the data - this is out of my control and won't change. The tables concerned are spread across several database servers, some of which the web server physically doesn't have access to, but the database server it does have access to has cross-server querying access to all the servers (hence another reason to use SPs as, AFAIK, LINQ, etc can't do cross-server joins) so if I do pull table definitions from my development environment (where, obviously, I do have access to all the tables) these won't be accessible in the production environment.
I could either create a flattened dataset of all the data then read through it, manually detecting the grouping changes (Shipment Number, OrderNumber) and then write my code accordingly, but then this doesn't make use of OOP and seems a large step backwards, plus it doesn't supply a reusable object for other, future, requirements. Instead, I could write code to go through the datasets and load the data manually into these objects but I figure why reinvent the wheel - surely this isn't an unusual requirement and there must be a way to do this?
Thanks - MH