外键定义为数据注释但仍返回 null
Foreign key is defined as data annotation but still returning null
我试图在 Blazor
页面中加入 tables,就像我以前在 Laravel Eloquent 中所做的那样,但不知何故无法用不同的方法解决FK 关系的数据类型,但现在我已经使数据类型相同,然后使用数据注释来定义外部,但 属性 仍然没有我提到的 table 模型的行。
我的代码 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; }
}
我试图在 Blazor
页面中加入 tables,就像我以前在 Laravel Eloquent 中所做的那样,但不知何故无法用不同的方法解决FK 关系的数据类型,但现在我已经使数据类型相同,然后使用数据注释来定义外部,但 属性 仍然没有我提到的 table 模型的行。
我的代码 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; }
}