Why does LINQ throw a NotSupportedException?

150 views Asked by At

Let's say we have a table called Car with columns such as ID, Identification, ModelName, OwnerId etc where OwnerId points to the primary key in the Owner table. This is all good, but then we want to add a Driver to the car, since we want to know who drives each car at a given time.

Sounds straight forward, right? Just create a Driver table and add a new nullable (there's no driver if the car is in the garage etc) int column called DriverId to the Car table, connect it with a foreign key and we're good to go.

I did this, and updated the EDMX in the model designer so the new table, column and foreign key showed up. All looks good. The DriverId property and the Driver navigation property are both there in the generated code and the new Driver class is also generated.

Now when I tried to use this new table and connect drivers to cars there's something very wrong. It looks like LINQ doesn't know about the DriverId column or the foreign key (navigation property) to Driver.

  1. If I try getting a car with a given driver:

    Car car = (from c in db.Cars.Where(x => x.DriverId == driverId) select c).FirstOrDefault();
    

    I expect to get a car if the driver is currently driving a car or null otherwise. What I get is this error message:

    System.NotSupportedException: The specified type member 'DriverId' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
    
  2. Let's say we want to add a new car with a driver:

    Car car = new Car{ blah, blah, etc, DriverId = driverId };
    db.Cars.Add(car);
    db.SaveChanges();
    

    This seems to work fine. The new car gets inserted into the database. The only thing is the DriverId column is null, so obviously it doesn't work fine...

I'm guessing these things are connected. I just don't see what the issue is. Anyone know why or got some suggestions as to what I can try?


EDIT:

The Car and Driver classes is purely generated code, so I'm sure they look familiar:

[DataContract(IsReference = true)]
[KnownType(typeof(Owner))]
[KnownType(typeof(Driver))]
public partial class Car
{
    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string Identification { get; set; }

    [DataMember]
    public Nullable<int> OwnerId { get; set; }

    [DataMember]
    public Nullable<int> DriverId { get; set; }

    //Navigation properties
    [DataMember]
    public virtual Owner Owner { get; set; }

    [DataMember]
    public virtual Driver Driver { get; set; }
}

[DataContract(IsReference = true)]
[KnownType(typeof (Car))]
public partial class Driver
{
    //Constructor
    public Driver()
    {
        this.Cars = new HashSet<Car>();
    }

    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string Name { get; set; }

    //Navigation properties
    [DataMember]
    public virtual HashSet<Car> Cars { get; set; }
}
1

There are 1 answers

2
quetzalcoatl On BEST ANSWER

Since the problem seems to be pinned and the work is in progrees, I'm pasting here trimmed discussion as a temporary "answer", just to leave the trace of work and to be sure that the comments with facts/thoughts don't not evaporate. Feel free to add/trim whatever here.
Eirik: if you manage to narrow it down to exact causes, please write a follow up answer explaining what was wrong and why did it compile silently with no errors. Just don't forget to "accept" it ;) Or if you like, add the info at the bottom of this "chatlog". I communitized this answer, so no points will be awarded.


Me: I too think one of your models went desynchronized. (..) One common thing was duplication of entries or not-updating the identifiers between model spaces. Could you try regenerating the EDMX from scratch, or do you have too many manual changes?


Eirik:

I've experienced bad updates earlier and solved them by removing the tables involved from the model and then running another update to get them readded. As for regenerating the EDMX from scratch...no. There's a bazillion manual changes, so I simply don't have the time to change it all back. I have already tried to remove the tables involved in this issue and readded them without luck. Same error/issue.


Me: Since nav-props are generated too, I assume you have a Driver property? Have you tried using objects instead? I mean, db.Cars.Where(x => x.Driver.Id == driverId) select c and Car car = new Car{ blah, blah, etc, Driver = driverObject };? If that worked, it'd indicate property<->column naming clash somewhere (...)


Eirik:

Changing x.DriverId == driverId to x.Driver.ID == DriverId results in the same error message (only initializers, members and navigation properties allowed). Driver should be recognized as a navigation property, but it isn't. DriverId should be recognized as a member, but it isn't. Insert also silently fails. That is, it inserts but with Car.DriverId being null.

Me: Nav props does not work. Your EDMX is screwed up, (..) I'd now chew through the three EDMX sections and verify all column, props, navs, and so on are correctly referring each other. Can also do another test: create a new edmx that contains only these tables, dont touch it, leave generated names and (..) compare contents of the new edmx with old big edmx and look for difference. (..)


Eirik:

I've created a new test project and generated the EDMX from scratch and stuff works as intended. So I'm guessing this is just a really bad case of update mess in the model designer. I'm currently going through both EDMX files and comparing the content. No solution yet, but I suspect I will find it eventuelly...zzZzzZ..thanks for the input so far!