EF中的相关实体缺失

问题描述 投票:1回答:3

我如何在一个视图中复制我的表信息,我想要显示我的球员俱乐部而不是他的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但我没有任何变种我需要做什么......没有clubname它的作品很棒enter image description here

c# asp.net-mvc entity-framework
3个回答
1
投票

将BasketBallClubId设置为playerIndex模型中的外键,该模型将引用BasketBallClub模型

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

现在您可以访问BasketBall请参阅模型的名称,因为您还没有在这里包含模型,希望您知道如何做


1
投票
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);
}

流利的Api示例。 Tutorial Here

public class BasketDbContext : DbContext
{
    public BasketDbContext()
        : base("BasketDb")
    {

    }

    public DbSet<Player> Player { get; set; }
    public DbSet<BasketBallClub> BasketBallClub { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new PlayerMap());
        modelBuilder.Configurations.Add(new BasketBallClubMap());
    }

}

using System.Data.Entity.ModelConfiguration;
public class PessoaMap : EntityTypeConfiguration<Player>
{
    public PlayerMap()
    {
        // Primary Key
        HasKey(t => t.Id);

        // Properties
        Property(t => t.PlayerName).HasMaxLength(50); //.HasColumnName("player_name");
        Property(t => t.PlayerSurname).HasMaxLength(50);
        Property(t => t.PlayerWeight);
        //..... configure all columns 

        //Relationships

        HasRequired(t => t.BasketBallClub)
                .WithMany(t => t.Players)
                .HasForeignKey(d => d.BasketBallClubId)
                .WillCascadeOnDelete(false);
    }
}

using System.Data.Entity.ModelConfiguration;
public class BasketBallClubMap : EntityTypeConfiguration<BasketBallClub>
{
    public BasketBallClubMap()
    {
        // Primary Key
        HasKey(t => t.Id);

        // Properties
        Property(t => t.Id); //.HasColumnName("id");
        Property(t => t.ClubName).HasMaxLength(50); //.HasColumnName("club_name");
    }
}

// Your Model
public class PlayerIndex1Model
{
    public int Id { get; set; }
    public string PlayerName { get; set; }
    public string PlayerSurname { get; set; }
    //... omitted other columns 
    public int BasketBallClubId { get; set; }
    public virtual BasketBallClub BasketBallClub { get; set; }
}

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

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

0
投票

这是我的代码Index1 ActionResult

 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);
        }

PlayerIndex1Model.cs

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;
        }
    }
}

和cshtml

@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>
© www.soinside.com 2019 - 2024. All rights reserved.