外键被定义为数据注释,但仍然返回null



我试图在Blazor页面中加入表,因为我总是在Laravel Eloquent中这样做,但不知怎的,无法与FK关系的不同数据类型一起工作,但现在我已经使数据类型相同,然后使用数据注释来定义外部,但仍然该属性没有我提到的表模型行。

我的tblEmployees代码-

public partial class TblEmployee
{
public TblEmployee()
{
Employeefrcs = new HashSet<Employeefrc>();
TblEmployeeDesignations = new HashSet<TblEmployeeDesignation>();
}
public string EmployeeCode { get; set; }
public string DepartmentCode { get; set; }
// ... irrelevant properties ...
[ForeignKey("Designation_Code")]
public TblDesignation Designation { get; set; }
public virtual TblDepartment DepartmentCodeNavigation { get; set; }
public virtual ICollection<Employeefrc> Employeefrcs { get; set; }
public virtual ICollection<TblEmployeeDesignation> TblEmployeeDesignations { get; set; }
}

和我的tblDesignations代码-

public partial class TblDesignation
{
public TblDesignation()
{
TblEmployeeDesignations = new HashSet<TblEmployeeDesignation>();
TblTeacherWorkLoads = new HashSet<TblTeacherWorkLoad>();
}
public int DesignationCode { get; set; }
// ... irrelevant properties ...    
public IEnumerable<TblEmployee> TblEmployees { get; set; }
public virtual ICollection<TblEmployeeDesignation> TblEmployeeDesignations { get; set; }
public virtual ICollection<TblTeacherWorkLoad> TblTeacherWorkLoads { get; set; }
}

和我的代码检索模型,如-

<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Cnic</th>
<th scope="col">Phone</th>
<th scope="col">Designation</th>
</tr>
</thead>
<tbody>
@{int i = 1}
@foreach (var employee in employees ?? new List<TblEmployee>())
{
<tr>
<th scope="row">@i</th>
<td>@employee.EmployeeName</td>
<td>@employee.CnicNo</td>
<td>@employee.CellNo</td>
<td>@(employee.Designation?.DesignationName ?? "Nandla")</td>
@{i++}
</tr>
}
</tbody>
</table>    

我的模型生成器有以下配置-

modelBuilder.Entity<TblEmployee>(entity =>
{
entity.HasKey(e => e.EmployeeCode).HasName("PK_dbo.tblEmployees");
entity.ToTable("tblEmployees");
entity.HasIndex(e => e.DepartmentCode, "IX_Department_Code");

// ... irrelevant properties configuration ...  
entity.HasOne(d => d.DepartmentCodeNavigation)
.WithMany(p => p.TblEmployees)
.HasForeignKey(d => d.DepartmentCode)
.HasConstraintName("FK_dbo.tblEmployees_dbo.tblDepartment_Department_Code");
});

我的tbldesignation在模型生成器是-

modelBuilder.Entity<TblDesignation>(entity =>
{
entity.HasKey(e => e.DesignationCode).HasName("PK_dbo.tblDesignations");
entity.ToTable("tblDesignations");

// ... irrelevant properties configuration ...  
});

仅使用DataAnnotations。为了简洁,删除了其他属性。

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class TblEmployee
{
public int DesignationCode { get; set; }
[ForeignKey(nameof(DesignationCode))]
public TblDesignation Designation { get; set; }
}
public partial class TblDesignation
{
[Key]
public int DesignationCode { get; set; }
[InverseProperty(nameof(TblEmployee.Designation))]
public IEnumerable<TblEmployee> TblEmployees { get; set; }
}

最新更新