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.
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.
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; }
}
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:
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
andCar car = new Car{ blah, blah, etc, Driver = driverObject };
? If that worked, it'd indicate property<->column naming clash somewhere (...)Eirik:
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: