locked
EF Query // Search // Foreign Key Problem RRS feed

  • Question

  • Hello !

    I got 2 Tables.

    The first Table is Category and the second is Topics.
    Category 1:N Topics  (One Category can have XXX Topics...)

    The Topic table got a Foreign Key called CategoryID.
    I made the association in the visual studio.

    My Problem :

    I want to select ALL Topics of ONE Category.

    In Linq 2 Sql I have done this with:
    int x = int.parse(DropDownList1.SelectedValue.ToString());
     LinqDataSource2.Where = "CategoryID==" + x;

    In EF I can't see and I can't access the foreign Key...

    I tryed

     var query = (from p in db.Categorys
                       where p.ID == x
                       select p).FirstOrDefault();
    x is the CategoryID Value : int x = int.Parse(DropDownList2.SelectedValue.ToString());

    The Query result is ok, i got there alle needed Information but I can't write something like

    EntityKnowledge.Where = "it.CategoryID ==" +x;
    because, I can't access the CategoryID......


    Please help me !

    Thanks a lot !




    Thursday, October 1, 2009 4:47 PM

Answers

  • EF v1 supports independent association which means a foreign key column can either be exposed as a simple property or as a navigation property but not as both except for one case where primary key is also the foreign key column in which it will expose the property and a navigation property. Anyways say i want to retrieve products that belong to category 1 this is what i would do

    db.Products.Where(p => p.Category.CategoryId == 1);
    not

    db.Products.Where(p => p.CategoryId == 1)

    second example though would work in vs2010 because it supports foreign key association. Although their are still certain scenarios where foreign key won't be possible such as many-to-many and tables that are structured whose foreign key resides in a separate table for normalization purpose.

    Zeeshan
    • Marked as answer by Diräkt Friday, October 2, 2009 6:12 AM
    Thursday, October 1, 2009 5:06 PM

All replies

  • EF v1 supports independent association which means a foreign key column can either be exposed as a simple property or as a navigation property but not as both except for one case where primary key is also the foreign key column in which it will expose the property and a navigation property. Anyways say i want to retrieve products that belong to category 1 this is what i would do

    db.Products.Where(p => p.Category.CategoryId == 1);
    not

    db.Products.Where(p => p.CategoryId == 1)

    second example though would work in vs2010 because it supports foreign key association. Although their are still certain scenarios where foreign key won't be possible such as many-to-many and tables that are structured whose foreign key resides in a separate table for normalization purpose.

    Zeeshan
    • Marked as answer by Diräkt Friday, October 2, 2009 6:12 AM
    Thursday, October 1, 2009 5:06 PM
  • Thank you for informations.

    My foreign Key is automaticly -> a navigation property. It gives an option with I can use it as a normal Property ?!

    But I don't understand how I filter my Data.

    The Table Topics got the Foreign Key CategoryID. If I can't access this data, so I can't filter for my Categorys.

    I want to do this :

    EnitityDATASOURCE.WHERE = "it.CategoryID ==" + x ;

    But I can't access the CategoryID.

    Sorry my bad english :-)

    Greetings
    Thursday, October 1, 2009 5:32 PM
  • zeeshan hirani now I think, I understand what you mean.

    It works THX
    Friday, October 2, 2009 6:12 AM