我正在尝试打印出使用多个表中数据的用户配置文件(我正在使用实体框架)。
我没有收到代码上的任何错误,但是当我加载页面时,它给了我这个错误
实体或复杂类型'Ability_Examen_ASP.Models.Teacher'不能 在LINQ to Entities查询中构建。
我想出一个解决方案,但我似乎无法弄明白。我是C#的新手,我真的不知道如何解决这个问题。
期待任何帮助,提前谢谢!
这是我正在使用的所有代码
模型
public class ProfileViewModel
{
public IEnumerable<Teacher> Teachers { get; set; }
public IEnumerable<Skill> Skills { get; set; }
}
控制器
public ActionResult MyProfile()
{
var id = Convert.ToInt32(Session["Teacher_ID"]);
if (id != null)
{
var teacher = (from t in db.Teachers
where t.ID == id
select new Teacher
{
FirstName = t.FirstName,
LastName = t.LastName,
Campus = t.Campus,
Email = t.Email,
Password = t.Password
});
if (teacher == null)
{
return HttpNotFound();
}
var teacherskills = (from ts in db.TeacherSkills
join s in db.Skills on ts.Skill_ID equals s.ID
where ts.Teacher_ID == id
select s);
var model = new ProfileViewModel
{
Skills = teacherskills.ToList(),
Teachers = teacher.ToList(),
};
return View(model);
}
return RedirectToAction("Login", "Auth");
}
数据库
查看
@model Ability_Examen_ASP.Models.ProfileViewModel
@{
ViewBag.Title = "My Profile";
}
<h2>Details</h2>
<div>
<h4>Teacher</h4>
<hr />
<dl class="dl-horizontal">
<dt>
Naam
</dt>
<dd>
@foreach (var teacher in Model.Teachers)
{
@teacher.FirstName @teacher.LastName
}
</dd>
<dt>
Email
</dt>
<dd>
@foreach (var teacher in Model.Teachers)
{
@teacher.Email
}
</dd>
<dt>
Passwoord
</dt>
<dd>
@foreach (var teacher in Model.Teachers)
{
@teacher.Password
}
</dd>
<dt>
Campus
</dt>
<dd>
@foreach (var teacher in Model.Teachers)
{
@teacher.Campus
}
</dd>
<dt>
Skills
</dt>
<dd>
@foreach (var skill in Model.Skills)
{
@skill.SkillName
}
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit Profile", "EditProfile") |
@Html.ActionLink("Edit Skills", "AddSkills")
</p>