0

I have the following Linq statement:

car car = db.cars
              .Include("wheels")
              .Include("windows")
              .Where(c => c.id == id)
              .FirstOrDefault();

I have lazy loading turned off. I want to return a single car with several wheels and several windows, but this returns a one wheeled car with just one window. How can I return all the children in this query, whilst still returning just one car?

I should also note that this is a many-to-many relationship, so a wheel can be assigned to many cars.

trees_are_great
  • 3,881
  • 3
  • 31
  • 62
  • Are you sure there isn't just one applicable wheel and window? I've just done the equivalent code about 5 minutes ago and it worked fine. What SQL query is it producing? – Jon Hanna Jun 24 '15 at 12:44
  • Thanks for checking. I have done several tests and have followed through carefully with debugger. I then try to do a delete and it fails if there is more than wheel for the car because of reference constraint error. I will have a look at the SQL now. Note that I am using Sybase Ase as a database (urgh!), so it could be a bug with the connector. – trees_are_great Jun 24 '15 at 12:57
  • 1
    I wasn't even checking, I just had the same case in my own code, but it was using PostgreSQL via Npgsql. – Jon Hanna Jun 24 '15 at 13:00
  • Ah right, I am using the Sybase connector with Sybase ASE. I have not been able to find the SQL generated for this unfortunately. I do not have a profiler, may look into this. – trees_are_great Jun 24 '15 at 13:19
  • Assing an `Action` to `db.Database.Log` that logs the strings passed and you should be able to see the SQL generated that way. – Jon Hanna Jun 24 '15 at 13:21
  • Is this only supported by EF6? Sybase-ase is restricted to version 4.something of entity framework – trees_are_great Jun 24 '15 at 13:25
  • Ah, alas it seems that `Log` was indeed added with EF4. A shame, since as you can imagine it's very useful at times like this. – Jon Hanna Jun 24 '15 at 13:38
  • I can imagine.. I'm dreaming of a day when my boss agrees that Sybase Ase needs to be replaced. Thanks for your help with this. – trees_are_great Jun 24 '15 at 13:52

1 Answers1

0

A bit of a hack to get around some Sybase Ase issues when used with the Sybase data connector, but this works:

var carSet = db.cars
              .Include("wheels")
              .Include("windows")
              .Where(c => c.id == id);

var car = carSet.FirstOrDefault();
trees_are_great
  • 3,881
  • 3
  • 31
  • 62