EF中的相关实体缺失

时间:2018-03-02 09:45:40

标签: c# asp.net-mvc entity-framework

我如何在一个视图中复制我的表信息,我想要显示我的玩家俱乐部而不是他的club_id:enter image description here

我坐着,不明白最后几天怎么做,我希望看到这样的结果: enter image description here

这是我的2张图片: enter image description here

我的ActionResult Index1(): enter image description here

当我运行它时,c#抛出错误 enter image description here

我知道错误就在这一行   ClubName = x.BasketballClub.ClubName 但我没有任何变种我需要做的...... 没有俱乐部名称,它的作品很棒 enter image description here

3 个答案:

答案 0 :(得分:1)

将BasketBallClubId设置为将引用的playerIndexModel中的ForeignKey    BasketBallClub模特

  [ForeignKey("BasketBallClub ")]
    public int BasketBallClubId { get; set; }
    public virtual BasketBallClub BasketBallClub { get; set;

现在您可以访问BasketBall请查看模型的名称,因为您没有在此处包含模型,希望您了解如何操作

答案 1 :(得分:1)

$action = isset($_GET['action']) ? $_GET['action'] : '';

流利的Api示例。 Tutorial Here

public class PlayerIndex1Model
{
    [Key]
    public int Id { get; set; }

    //Why this ???
    public List<Player> Players { get; set; } 

    public string PlayerName { get; set; }
    public int PlayerWeight { get; set; }
    public string PlayerSurname { get; set; }
    public DateTime Birthday { get; set; }
    public string PlayerPosition { get; set; }
    public decimal PlayerHeight { get; set; }

    public string ClubName { get; set; }



    [ForeignKey("BasketBallClub")]
    public int BasketBallClubId { get; set; }
    public virtual BasketBallClub BasketBallClub
    {
        get; set;
    }
}

public class BasketBallClub
{
    [Key]
    public int Id { get; set; }
    public string ClubName { get; set; }

    public virtual ICollection<Player> Players { get; set; } 
}


public ActionResult Index1()
{
    List<Player> playerList =  new List<Player>();

    BasketDbContext db = new BasketDbContext();
    List<Player> playerList = db.Player.ToList(); 

    /* if not load BasketballClub try this way
    var playerList = new List<Player>();
    using (var db = new BasketDbContext())
    {
        playerList = db.Player.Include(x=>x.BasketballClub).ToList();  
    }
    */

    PlayerIndex1Model playerVM = new PlayerIndex1Model();

    List<PlayerIndex1Model> playerVMList = playerList.Select(x => new PlayerIndex1Model
    {
        PlayerName = x.PlayerName,
        Id = x.Id,
        PlayerHeight=x.PlayerHeight,
        PlayerSurname=x.PlayerSurname,
        PlayerPosition=x.PlayerPosition,
        Birthday=x.Birthday,

        PlayerWeight=x.PlayerWeight,
        BasketBallClubId = x.BasketballClubId,
        ClubName = x.BasketBallClub?.ClubName ?? "" //if BasketballClub is not null returns ClubName otherwise ""

    }).ToList();

    return View(playerVMList);
}

答案 2 :(得分:0)

这是我的代码 Index1 ActionResult

&#13;
&#13;
 public class PlayerController : Controller
    {
        public ActionResult Index1()
        {
            BasketDbContext db = new BasketDbContext();
            List<Player> playerList = db.Player.ToList();

            PlayerIndex1Model playerVM = new PlayerIndex1Model();

            List<PlayerIndex1Model> playerVMList = playerList.Select(x => new PlayerIndex1Model
            {

                PlayerName = x.PlayerName,
                Id = x.Id,
                PlayerHeight=x.PlayerHeight,
                PlayerSurname=x.PlayerSurname,
                PlayerPosition=x.PlayerPosition,
                Birthday=x.Birthday,
                
                PlayerWeight=x.PlayerWeight,
                BasketBallClubId = x.BasketballClubId,
                ClubName = x.BasketballClub.ClubName
                                  
            }).ToList();

            return View(playerVMList);
        }
&#13;
&#13;
&#13;

PlayerIndex1Model.cs

&#13;
&#13;
using Basket.Data.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;


namespace Basket.Web.Models
{
    public class PlayerIndex1Model
    {
        public int Id { get; set; }
        public List<Player> Players { get; set; }

        public string PlayerName { get; set; }
        public int PlayerWeight { get; set; }
        public string PlayerSurname { get; set; }
        public DateTime Birthday { get; set; }
        public string PlayerPosition { get; set; }
        public decimal PlayerHeight { get; set; }

        public string ClubName { get; set; }

        //fk

        [ForeignKey("BasketBallClub")]
        public int BasketBallClubId { get; set; }
        public virtual BasketBallClub BasketBallClub
        {
            get; set;
        }
    }
}
&#13;
&#13;
&#13;

和cshtml

&#13;
&#13;
@model IEnumerable<Basket.Web.Models.PlayerIndex1Model>
    @{
        ViewBag.Title = "Index1";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h2>Index1</h2>

    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.PlayerName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PlayerWeight)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PlayerSurname)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Birthday)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PlayerPosition)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PlayerHeight)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ClubName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.BasketBallClubId)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.PlayerName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PlayerWeight)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PlayerSurname)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Birthday)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PlayerPosition)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PlayerHeight)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ClubName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.BasketBallClubId)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                    @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                </td>
            </tr>
        }

    </table>
&#13;
&#13;
&#13;