entity framework - Why does LINQ throw a NotSupportedException? -


let's have table called car columns such id, identification, modelname, ownerid etc ownerid points primary key in owner table. good, want add driver car, since want know drives each car @ given time.

sounds straight forward, right? create driver table , add new nullable (there's no driver if car in garage etc) int column called driverid car table, connect foreign key , we're go.

i did this, , updated edmx in model designer new table, column , foreign key showed up. looks good. driverid property , driver navigation property both there in generated code , new driver class generated.

now when tried use new table , connect drivers cars there's wrong. looks linq doesn't know driverid column or foreign key (navigation property) driver.

  1. if try getting car given driver:

    car car = (from c in db.cars.where(x => x.driverid == driverid) select c).firstordefault(); 

    i expect car if driver driving car or null otherwise. error message:

    system.notsupportedexception: specified type member 'driverid' not supported in linq entities. initializers, entity members, , entity navigation properties supported. 
  2. let's want add new car driver:

    car car = new car{ blah, blah, etc, driverid = driverid }; db.cars.add(car); db.savechanges(); 

    this seems work fine. new car gets inserted database. thing driverid column null, doesn't work fine...

i'm guessing these things connected. don't see issue is. know why or got suggestions can try?


edit:

the car , driver classes purely generated code, i'm sure 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 problem seems pinned , work in progrees, i'm pasting here trimmed discussion temporary "answer", leave trace of work , sure comments facts/thoughts don't not evaporate. feel free add/trim whatever here.
eirik: if manage narrow down exact causes, please write follow answer explaining wrong , why did compile silently no errors. don't forget "accept" ;) or if like, add info @ bottom of "chatlog". communitized answer, no points awarded.


me: think 1 of models went desynchronized. (..) 1 common thing duplication of entries or not-updating identifiers between model spaces. try regenerating edmx scratch, or have many manual changes?


eirik:

i've experienced bad updates earlier , solved them removing tables involved model , running update them readded. regenerating edmx scratch...no. there's bazillion manual changes, don't have time change back. have tried remove tables involved in issue , readded them without luck. same error/issue.


me: since nav-props generated too, assume have driver property? have tried using objects instead? mean, db.cars.where(x => x.driver.id == driverid) select c , car car = new car{ blah, blah, etc, driver = driverobject };? if worked, it'd indicate property<->column naming clash somewhere (...)


eirik:

changing x.driverid == driverid x.driver.id == driverid results in same error message (only initializers, members , navigation properties allowed). driver should recognized navigation property, isn't. driverid should recognized member, isn't. insert silently fails. is, inserts car.driverid being null.

me: nav props not work. edmx screwed up, (..) i'd chew through 3 edmx sections , verify column, props, navs, , on correctly referring each other. can test: create new edmx contains these tables, dont touch it, leave generated names , (..) compare contents of new edmx old big edmx , difference. (..)


eirik:

i've created new test project , generated edmx scratch , stuff works intended. i'm guessing bad case of update mess in model designer. i'm going through both edmx files , comparing content. no solution yet, suspect find eventuelly...zzzzzz..thanks input far!


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -