2012-07-30 59 views
1

我有一個名爲Students的表和一個名爲Majors,Students和Majors的表加入了MajorId我已經設置了這個關係並已在模式中設置了外鍵。當我訪問我的Student對象時,如何返回MajorName列(這來自Majors表)?我在intellisense中唯一的選擇是Major_1,Major_1Reference,MajorId在實體框架中加入表

+0

http://stackoverflow.com/questions/21051612/entity-framework-join-3-tables – 2017-02-11 05:59:34

回答

2

Major_1應該是一個導航屬性導致相應Major項,所以你應該能夠訪問這樣的Major的屬性:

from s in ctx.Students 
select s.Major_1.MajorName 
0

您可以使用LINQ連接語句是這樣做的查詢在兩張桌子上...

var q = from s in Students 
     join m in Majors on s.MajorId equals m.MajorId 
     select new { m.MajorName };