[英]When I create a list that holds the “id” values of other database tables, I cannot reach the “name” values for the related tables
我有一堂課叫做“ ODL”
其中包含以下值:
{
public int ODLId { get; set; }
public string ODLName { get; set; }
public int? RefODLId { get; set; }
public int VehicleTypeId { get; set; }
public int MotorTypeId { get; set; }
public int GearboxTypeId { get; set; }
public int DoorId { get; set; }
public int VehicleLengthId { get; set; }
public string ProjectName { get; set; }
}
在另一個名為“ ODLModel”的類中,用作模型類。 在此Model類中,有一個“列表ODL”屬性。
因此,現在我可以在foreach循環中獲取VehicleType,MotorType等的ID值。
@foreach (var odl in Model.ODLs)
{
<tr>
<td>@odl.ODLName</td>
<td>@odl.VehicleLengthId</td>
<td>@odl.MotorTypeId</td>
!!! In here I dont want to show Id values but I would like to reach
the related tables VehicleLength.cs etc and take their "Name" values
and print it in here !!!
.....
</tr>
}
</tbody>
如我所說,我想在VehicleType.cs,MotorType.cs等中顯示“名稱”值。
我正在使用EntityFramework,我在ODL.cs中作為ID屬性引用的所有類都與一個表相關。
public DbSet<VehicleType> VehicleTypes { get; set; }
public DbSet<MotorType> MotorTypes { get; set; }
public DbSet<GearboxType> GearboxTypes { get; set; }
public DbSet<Door> Doors { get; set; }
public DbSet<VehicleLength> VehicleLengths { get; set; }
public DbSet<ODL> ODLs { get; set; }
為了獲得相關項目的屬性,您必須更改ODL類。 無需聲明int ID
屬性,而是需要聲明“導航屬性”。 您可以在官方文檔中閱讀有關它的更多信息。您的ODL類如下所示:
{
public int ODLId { get; set; }
public string ODLName { get; set; }
public int? RefODLId { get; set; }
public virtual VehicleType VehicleType { get; set; }
public virtual MotorType MotorType { get; set; }
public virtual GearboxType GearboxType{ get; set; }
public virtual Door Door { get; set; }
public virtual VehicleLength VehicleLength { get; set; }
public string ProjectName { get; set; }
}
您將可以這樣稱呼它:
var odl = ... // get ODL entity from DbSet
odl.VehicleType.Name
odl.GearboxType.SomeProperty
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.