Data Platform Developer Center > Data Platform Development Forums > ADO.NET Entity Framework and LINQ to Entities > Columns having Referential Integrity/Foreign Key relation ship are not shown in EDM. Why?
Ask a questionAsk a question
 

AnswerColumns having Referential Integrity/Foreign Key relation ship are not shown in EDM. Why?

  • Thursday, October 29, 2009 7:01 AMJai Mallesh Babu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi All,

    Columns having Referential Integrity/Foreign Key relation ship are not shown in EDM. Why?
    When I remove referential integrity, all the properties are shown in EDM. However, when I add referential integrity in database, the respective columns are not shown in EDM.

    Why is it? How to fix this problem.

    Regards,
    Jai

Answers

  • Thursday, October 29, 2009 7:48 AMGil FinkMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,
    In EF1 the behavior is to create a navigation property between entities with foreign keys. That mean that you won't see the foreign key in the EDM but it exists.
    In EF4 you have both navigation properties and also forreign keys as part of the EDM.

    I hope it will help you.
    Gil Fink
  • Thursday, October 29, 2009 7:57 AMLingzhi SunMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Jai,

     

    Gil is definitely correct!  In the current version of Entity Framework (.NET 3.5 SP1), the foreign key relationship columns are defined as navigation properties.  Unlike LINQ to SQL data class, we cannot have the foreign key column property in the entity class in current version of EF.   

     

    As Gil has said, in Entity Framework 4.0 (Visual Studio 2010), we can have a new association named foreign key association, which allows us to keep the foreign key column property in the entity class.  For detail, please see http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx. 

     

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Thursday, October 29, 2009 7:48 AMGil FinkMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,
    In EF1 the behavior is to create a navigation property between entities with foreign keys. That mean that you won't see the foreign key in the EDM but it exists.
    In EF4 you have both navigation properties and also forreign keys as part of the EDM.

    I hope it will help you.
    Gil Fink
  • Thursday, October 29, 2009 7:57 AMLingzhi SunMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Jai,

     

    Gil is definitely correct!  In the current version of Entity Framework (.NET 3.5 SP1), the foreign key relationship columns are defined as navigation properties.  Unlike LINQ to SQL data class, we cannot have the foreign key column property in the entity class in current version of EF.   

     

    As Gil has said, in Entity Framework 4.0 (Visual Studio 2010), we can have a new association named foreign key association, which allows us to keep the foreign key column property in the entity class.  For detail, please see http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx. 

     

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Thursday, October 29, 2009 4:16 PMJai Mallesh Babu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for reply Lingzhi ang Gil.

    So in case if we release our product with 3.5v, there is no way to get those missing properties which have Foreign key relationships?
    At this time, we are removing all relation ships in database which is a bad idea.

    I understand that upgrading to 4.0v is the only option. Let me know if you have any alternatives.

    Thank you,
    Jai
  • Thursday, October 29, 2009 5:22 PMDaniel Ianegitz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Jai

    You can navigate betwenn FK fields by the parent references, just like:

    var

     

     

    q = from p in db.TBLProduct
    select new
    {
        p.IDProduct,
        p.TBLCategory.IDCategory
    }

    using this way, you can navigate not only in the FK field, but in all fields of the child table!

    Expect be helpfull!!
  • Friday, October 30, 2009 10:23 AMLingzhi SunMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Jai,

     

    Daniel is right!  We can get the value of the foreign key column value by the navigation properties. 

     

    For detail, please see How to: Navigate Relationships Using Navigation Properties (Entity Framework).  

     

    Please feel free to let me know if you need any further assistance.

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Wednesday, November 04, 2009 1:12 AMLingzhi SunMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Jai,

     

    Do you need any further assistance?  If so, please feel free to let me know.
     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Wednesday, November 04, 2009 6:11 AMJai Mallesh Babu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I think you can close this thread.

    Thanks for answers.

    Regards,
    Jai