locked
Which is best way to join by foreign key using EF ? RRS feed

  • Question

  • Hello

    This is what I would normally do using LINQ and I want to do the same thing using EF:

    Thank You

    from _r in db.players
    
    join _c in db.playerCategories on _r.PlayerId equals _c.PlayerId
    

    Sunday, August 15, 2010 11:33 PM

Answers

  • Looks like in 4.0 version there's a “Include Foreign Key Columns in the Model” checkbox. If checked it let's me play with FK's and this solves all my problems. 

    I love EF again now :)

    Monday, August 16, 2010 9:06 PM

All replies

  • Hi,

    The same syntax is still valid for Entity Framework. However, if you have relationships defined in your Entity Model, you can directly use the navigational property without having to worry about foreign key relationships.

    e.g.

    from _r in db.Players
     from _c in _r.PlayerCategories
     where _c.SomeField == ''
    select _r
    

    Regards,


    Syed Mehroz Alam
    My Blog | My Articles
    • Proposed as answer by DK. Da Monday, August 16, 2010 2:47 AM
    Monday, August 16, 2010 2:32 AM
  • By doing this I get an exception of type System.Data.EntityCommandExecutionException.

    Any ideas ?


    Monday, August 16, 2010 1:39 PM
  • Looks like in 4.0 version there's a “Include Foreign Key Columns in the Model” checkbox. If checked it let's me play with FK's and this solves all my problems. 

    I love EF again now :)

    Monday, August 16, 2010 9:06 PM