none
One to one relationship in LINQ RRS feed

  • Question

  • Hello,

    I am using VS 2008 Team system edition for one of my project and we are using LINQ to sql as our dataacess layer.

    We are facing problems when there is one to one relation between the tables. e.g

    customer table
    customerid - PK
    name

    addresstable
    addressid - PK
    addressline1
    customerid - FK and unique. (this makes sure that relation is one to one)

    now when i drag drop these tables on the linq designer surface it generates a member
    private EntitySet<Address> _Addresses;
    in customers table.

    However I expect it to be EntityRef<Address> _Address which is logical for one to one relation.

    Is there some way the designer can recognise from the table structure that the relation is one to one. Ofcouse I can always change the relation from the designer and in that case everything works fine. But I want to know is there any way to specify one to one relation in the sql server so that the linq designer can recognise it and generate proper code.

    -Amit
    Thursday, July 31, 2008 6:00 AM

Answers

  • LINQ to SQL understands 1:1 relationships that are essentially defined as Primary Key to Primary Key relationships - so in both fields, you have PKs - but the PK of the child is also the FK representing the parent.

     

    So in your example, you could have CustomerID as the PK in Address and also as the FK pointing to the PK in Customer.

     

    Hope this makes sense.

     

    Faisal Mohamood | Program Manager | LINQ to SQL

    Friday, August 1, 2008 5:40 PM
    Moderator