[英]Passing generated id from table 1 (primary key) to table 2 (foreign key)?
[英]Getting Data from foreign table by primary key
在此設置中,我有一個所有者,他有10所學校。 我在一個組合框中獲得了所有者列表,並使用combox.selectedvalue
選擇具有ID的所有者,並使用其文本值選擇名稱。 現在,我有一個數據網格視圖,在其中要列出所有屬於選定所有者的學校,並在數據網格視圖的“所有者”表中列出所有者名稱的附加列。
我想使用實體框架做到這一點。
我在這里看到了很多問題,但沒有找到我真正需要的東西。
取決於您的數據庫架構
在ComboBoxOwner_SelectedIndexChanged()
事件中嘗試此操作
var selectedOwner = ((Owner)ComboBoxOwner.SelectedItem).Id;
GridViewSchools.DataSource = (from school in DbContext.Schools
join owner in DbContext.Owners on school.OwnerId equals owner.Id
where school.OwnerId == selectedOwner
select new { schoolName= school.Name , OwnerName = owner.Name }).ToList();
您可以用相同的方法添加學校的所有信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.