我是ASP.NET MVC和Entity框架的新手。以前我曾经使用过ADO.NET Datatables和DataSet。因此,在单次调用中从数据库中获取多个表的场景中,我曾经使用过DataSet。 e.g。
ALTER PROCEDURE [dbo].[SpTest]
As
Begin
Select * from Table1
Select * from Table2
End
在C#上,我曾经做过
Dataset ds = DAL.GetDataSetBySp("SpTest");
Datatable dt1 = ds.Tables["Table1"];
Datatable dt2 = ds.Tables["Table2"];
现在如果我想在Single Db Call中使用Entity Framework的多个表,我该怎么办?
答案 0 :(得分:0)
你可以这样做,但表格中应该有关系:
public class Table1
{
[Key]
public int IDCol{ get; set; }
public string Col2{ get; set; }
}
public class Table2
{
[Key]
public int IDCol2{ get; set; }
public int IDCol{ get; set; }
public string Col3{ get; set; }
}
public class JoiningTable
{
public int IDCol1{ get; set; }
public int IDCol2{ get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
}
public List<JoiningTable> GetData()
{
List<JoiningTable> bothTablesData =
from t in Table1
join t2 in Table2
on t.IDCol equals t2.IDCol1
select new { IDCol1 = t.IDCol, Col3 = t2.Col3, Col2 = t.Col2 }.ToList<JoiningTable>();
return bothTablesData;
}