簡體   English   中英

在 Entity Framework Core 和 ASP.NET Core 中加載相關數據

[英]Loading related data in Entity Framework Core and ASP.NET Core

我正在嘗試使用Include()加載相關數據,但我認為我做錯了什么。

我想在這張桌子上加載鞋子的圖像,並將用於練習目的的圖像加載到一行中,但是當我添加ShoeImages.ImageURL (舉個例子)時,我收到錯誤 CS1061 並且我無法顯示來自Include()

我有一個索引頁:

@model IEnumerable<Shoes.Data.Entities.ShoeEntity>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Title)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Description)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShoesImages)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Price)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <!--- This is the problem too --->
            <td>
                @Html.DisplayFor(modelItem => item.ShoesImages.ImageURL)
            </td>

            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
            </td>
        </tr>
}
    </tbody>
</table>

索引控制器:

public IActionResult Index()
{
     var data = _context.Shoes.Include(ci => ci.ShoeImages);
     return View(data);
}

Shoe實體:

public class ShoeEntity
{
     public string Id { get; set; }
     public string Name { get; set; }
     public ICollection<ShoeImageEntity> ShoeImages { get; set; }
}

ShoeImages 實體:

public class ShoeImageEntity
{
    public string Id { get; set; }
    public string ImageURL { get; set; }

    public string ImageFullPath => 
            string.IsNullOrEmpty(ImageURL) ? null : $"https://localhost:44357{ImageURL.Substring(1)}";

    public ShoeEntity Shoe { get; set; }
}

上下文類:

public class DataContext : IdentityDbContext<UserEntity, RoleEntity, string>
{
     public DbSet<ShoeEntity> Shoes{ get; set; }
     public DbSet<ShoeImagesEntity> ShoesImages { get; set; }     

     public DataContext(DbContextOptions options) : base(options)
     {
     }
}

我還嘗試了實體框架文檔。

您的 ShoeImages 字段對應於多個 ImageURL 字段,因為它屬於 ICollection 類型。

您可以使用以下視圖代碼來顯示它們。

 @model IEnumerable<Shoes.Data.Entities.ShoeEntity> <p> <a asp-action="Create">Create New</a> </p> <table class="table"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.Title) </th> <th> @Html.DisplayNameFor(model => model.Description) </th> <th> @Html.DisplayNameFor(model => model.ShoesImages) </th> <th> @Html.DisplayNameFor(model => model.Price) </th> <th></th> </tr> </thead> <tbody> @foreach (var item in Model) { foreach (var images in item.ShoeImages) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => images.ImageURL) </td> <td> @Html.DisplayFor(modelItem => item.Price) </td> <td> <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> | <a asp-action="Details" asp-route-id="@item.Id">Details</a> | <a asp-action="Delete" asp-route-id="@item.Id">Delete</a> </td> </tr> } } </tbody> </table>

感謝大家的幫助。 如果我一個人做,我就不會注意到。 我檢查答案是正確的。 我將進行編輯,因為在每個 foreach 之前,兩個 foreach 都需要 @

 @model IEnumerable<Shoes.Data.Entities.ShoeEntity> <p> <a asp-action="Create">Create New</a> </p> <table class="table"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.Title) </th> <th> @Html.DisplayNameFor(model => model.Description) </th> <th> @Html.DisplayNameFor(model => model.ShoesImages) </th> <th> @Html.DisplayNameFor(model => model.Price) </th> <th></th> </tr> </thead> <tbody> @foreach (var item in Model) { @foreach (var images in item.ShoeImages) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => images.ImageURL) </td> <td> @Html.DisplayFor(modelItem => item.Price) </td> <td> <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> | <a asp-action="Details" asp-route-id="@item.Id">Details</a> | <a asp-action="Delete" asp-route-id="@item.Id">Delete</a> </td> </tr> } } </tbody> </table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM