Max
Max

Reputation: 7119

LINQ Get object across foreign key

I have a LINQ to SQL DBML file set up with associations between tables. I also have foreign keys set up in the database for these tables.

I was wondering if it's possible to do something like in django, where a foreign key relationship allows you to access the other "object" across that relationship.

For example, if I have a Customer table and an Address table, calling customer.Address would let me access the whole object across my AddressID foreign key so I wouldn't have to write another query to construct that object manually.

Is this possible in LINQ to SQL?

Thanks.

Upvotes: 0

Views: 1025

Answers (2)

Space Programmer
Space Programmer

Reputation: 78

Yes..that is possible, you can modify the DBML in the designer to create relationships as you like.

see: http://www.hookedonlinq.com/LINQtoSQL5MinuteOverview.ashx

Upvotes: 1

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

I don't know how to do this with DBML, but it most certainly is possible. The sqlmetal tool uses foreign keys from Foo to Bar to add a "Bar" property to the Foo class and a "Foos" collection property to the Bar class.

The XSD mentions an Association type. That's probably a good starting point. You could also point sqlmetal to your schema and tell it to spit out a DBML, to see how it handles foreign keys.

Upvotes: 1

Related Questions