各テーブルに対応する 2 つのテーブルとモデルがあります: Employee と EmployeeEducation では、EmployeeEducation テーブルから 2 つの外部キーがあります。教育ごとに異なるコンサルタントを配置できます。
[Required(ErrorMessage = "Contact Admin")]
[Display(Name = "Consultant")]
public int? ConsultantId { get; set; }
*emphasized text*
[Required(ErrorMessage = "Contact Admin")]
public int? EmployeeId { get; set; }
IDごとに、オブジェクトに到達するためにこれらのオブジェクトがあります
[ForeignKey("EmployeeId")]
public virtual Employee Employee { get; set; }
[ForeignKey("ConsultantId")]
public virtual Employee Consultant { get; set; }
コードを実行し、コンサルタントと一緒に従業員に教育を受けようとすると、次の例外が発生し、内部例外が発生します。
EntityCommandExecutionException
{"An error occurred while executing the command definition. See the inner exception for details."}
Inner exception: SqlCeException
{"The column name is not valid. [ Node name (if any) = Extent1,Column name = Employee_Id ]"}
しかし、コンサルタント オブジェクトを削除しても、例外は発生しません。この問題を解決して、コンサルタントと従業員の両方にアクセスできるようにするにはどうすればよいですか?
DetailsEducation.cshtml で例外が発生します。
@{ if (Model.EducationList == null || !Model.EducationList.Any())
{
以下は、EducationList の設定方法です。
public ActionResult DetailsEducation(int id)
{
Employee employee = _work.EmployeeRepository.GetSet()
.Include(a => a.EducationList)
.Include(a => a.EducationList.Select(c => c.University))
.Include(a => a.EducationList.Select(c => c.Department))
.FirstOrDefault(a => a.Id == id);
return PartialView("_DetailsEducation", employee);
}