开发者

LINQ Get object across foreign key

开发者 https://www.devze.com 2023-02-05 16:17 出处:网络
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 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 a开发者_开发问答llows 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号