locked
Eager Loading w/ Classic ADO.NET RRS feed

  • Question

  • User-221752185 posted

    Hello, I want to be able to eager load an entity using classic ADO.NET. We are moving away from Entity Framework. My thoughts were to UNION select statements for each related entity and then use reader.NextResult to populate the properties. However, I don't think that will work. Cause if one related entity set is empty, then this method wouldn't work. Or maybe it would? Any ideas on how to do this? Classic ADO.NET only!

    Wednesday, November 11, 2015 1:45 PM

Answers

  • User753101303 posted

    Hi,

    I! don't see why it would confuse NextResult.It will find an empty resultset (rather than no result set at all) if no rows are returned. Also you meant likely just multiple SELECT statements rather than a UNION (which return a single result set and would require the same column count and types making this almost impossible to use). Just try and report back if you can't make it to work.

    BTW AFAIK this is how EF7 works (if I remember the previous EF architecture had to generate a single SQL statement for each EF query so they had use a single JOIN based approach for eager loading).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 11, 2015 3:49 PM

All replies

  • User753101303 posted

    Hi,

    I! don't see why it would confuse NextResult.It will find an empty resultset (rather than no result set at all) if no rows are returned. Also you meant likely just multiple SELECT statements rather than a UNION (which return a single result set and would require the same column count and types making this almost impossible to use). Just try and report back if you can't make it to work.

    BTW AFAIK this is how EF7 works (if I remember the previous EF architecture had to generate a single SQL statement for each EF query so they had use a single JOIN based approach for eager loading).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 11, 2015 3:49 PM
  • User-221752185 posted

    Sorry, just remembered why I mentioned that there would be no result set at all versus an empty one. The reason is because I wanted to make the loading of each navigation property optional rather than load all of them or none of them  For instance, GetOrdersByCustomerId(int customerId, bool includeCustomer, bool includeProducts).  Is this crazy? Or do you think I should just load the whole graph or not at all? 

    I figured actually if the bit parameter is false for one of the navigation properties, i can still get the NextResult to return true but reader.Read() to return false with:

    SELECT * FROM (SELECT NULL) WHERE 0

    Haven't tested it but thats what I am thinking. 

    Wednesday, November 11, 2015 4:39 PM
  • User753101303 posted

    Never just load a whole graph. Always try to load only the data you actually need. Now it depends how you handle those boolean values. If you have a SELECT you'll have a result set (even if empty). Or a stored procedure could include an IF. Or you could send yourself multiple SELECT statements to the db in this case you'll have the result sets you asked for. So in all cases I don't see why you wouldn't be able to process each result set accordingly. I would suggest to just start an implementation and see if you have an actual problem.

    BTW if not done already make 100% sure moving entirely away from EF is your best path compared maybe with just solving a particular problem you may have...

    Wednesday, November 11, 2015 6:03 PM