0

我试图加入BlazorPages 中的表,就像我在 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 ...  
});
4

1 回答 1

0

仅使用 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; }
}

于 2021-03-04T06:19:25.377 回答