如何使用 Entity Framework 连接三个表来获取所有数据
How to fetch all data using join three tables using Entity Framework
我收到这个错误:
System.NullReferenceException: Object reference not set to an instance of an object
如何解决这个异常?
加入查询控制器:
var Cs = new List<MyModel>();
using (Join3Entities1 db = new Join3Entities1())
{
DateTime start1 = DateTime.Now;
ViewBag.Start = "Start Time :" + start1;
Cs = (from e in db.Students
join p in db.Marks on e.S_ID equals p.S_ID
join t in db.Details on p.School_ID equals t.School_ID
where p.Score > 50
select new MyModel
{
S_Name = e.S_Name,
Score = (int)p.Score,
Status = p.Status,
Address_City = t.Address_City,
Email_ID = t.Email_ID,
Accomplishments = t.Accomplishments
}).ToList();
DateTime end1 = DateTime.Now;
ViewBag.end = "End Time:" + end1;
TimeSpan time1 = end1 - start1;
ViewBag.time = "TimeSpan:" + time1;
}
return View();
上面的代码是加入三个table我写在controller部分
型号:
public class 我的模型
{
public 字符串 S_Name { 得到;放; }
public int Score { get;放; }
public 字符串状态 { 得到;放; }
public 字符串 Address_City { 得到;放; }
public 字符串 Email_ID { 得到;放; }
public 字符串成就 { get;放; }
}
查看:
@model IEnumerable<Join3table.Models.MyModel>
@{
ViewBag.Title = "Home Page";
}
@foreach (var per in Model)
{
<tr>
<td>@per.S_Name</td>
<td>@per.Score</td>
<td>@per.Status</td>
<td>@per.Address_City</td>
<td>@per.Email_ID </td>
<td>@per.Accomplishments</td>
</tr>
}
</tbody>
</table>
我用主键和外键关系创建了三个 tables student,mark 和 details
而不是 select new model
,尝试一个一个地返回每个 table 以查看 nullref 具体来自哪里,这样您就可以缩小范围。
另一个安全的选择是确保在调用每个列之前进行 nullrefcheck S_Name = e != null && !string.isNullOrEmpty(e.S_Name) ? e.S_Name : string.Empty;
我收到这个错误:
System.NullReferenceException: Object reference not set to an instance of an object
如何解决这个异常?
加入查询控制器:
var Cs = new List<MyModel>();
using (Join3Entities1 db = new Join3Entities1())
{
DateTime start1 = DateTime.Now;
ViewBag.Start = "Start Time :" + start1;
Cs = (from e in db.Students
join p in db.Marks on e.S_ID equals p.S_ID
join t in db.Details on p.School_ID equals t.School_ID
where p.Score > 50
select new MyModel
{
S_Name = e.S_Name,
Score = (int)p.Score,
Status = p.Status,
Address_City = t.Address_City,
Email_ID = t.Email_ID,
Accomplishments = t.Accomplishments
}).ToList();
DateTime end1 = DateTime.Now;
ViewBag.end = "End Time:" + end1;
TimeSpan time1 = end1 - start1;
ViewBag.time = "TimeSpan:" + time1;
}
return View();
上面的代码是加入三个table我写在controller部分
型号: public class 我的模型 { public 字符串 S_Name { 得到;放; } public int Score { get;放; } public 字符串状态 { 得到;放; } public 字符串 Address_City { 得到;放; } public 字符串 Email_ID { 得到;放; } public 字符串成就 { get;放; } }
查看:
@model IEnumerable<Join3table.Models.MyModel>
@{
ViewBag.Title = "Home Page";
}
@foreach (var per in Model)
{
<tr>
<td>@per.S_Name</td>
<td>@per.Score</td>
<td>@per.Status</td>
<td>@per.Address_City</td>
<td>@per.Email_ID </td>
<td>@per.Accomplishments</td>
</tr>
}
</tbody>
</table>
我用主键和外键关系创建了三个 tables student,mark 和 details
而不是 select new model
,尝试一个一个地返回每个 table 以查看 nullref 具体来自哪里,这样您就可以缩小范围。
另一个安全的选择是确保在调用每个列之前进行 nullrefcheck S_Name = e != null && !string.isNullOrEmpty(e.S_Name) ? e.S_Name : string.Empty;