繁体   English   中英

如何使用实体框架连接两个表并匹配外键

[英]How to join two tables using Entity Framework and match foreign keys

我是 C# 和实体框架的新手,所以我真的很困惑 - 对此表示歉意。

我目前有一个包含两个表ArtistsAlbums的数据库。

我想在页面上显示所有专辑的列表,并将相应的ArtistIDArtist页面上的外键)链接到它。

我正在尝试将这两个表连接在一起以在我的AllAlbums页面上呈现它们。

有人可以看看我的代码并指出我正确的方向吗?

目前,我的页面只渲染出所有专辑,与艺术家没有任何关系。

我将发布迄今为止我拥有的适当的代码片段。

AllAlbum.cshtml.cs

public class AllAlbumModel : PageModel
{
    DatabaseContext _Context;

    public AllAlbumModel(DatabaseContext databasecontext)
    {
        _Context = databasecontext;
    }

    public List<Album> AlbumList { get; set; }

    public void OnGet()
    {
        var data = (from albumlist in _Context.albums
                    select albumlist).ToList();

        AlbumList = data;
    }
}

专辑.cs

[Table("albums")]
public class Album
{
    [Key]
    public int AlbumId {get; set;}
    // [Required(ErrorMessage = "Enter Album ID")]
    public string Title {get; set;}
    // [Required(ErrorMessage = "Enter Title")]
    public int ArtistId {get; set;}
}

艺术家.cs

[Table("artists")]
public class Artist
{
    [Key]
    public int ArtistId {get; set;}
    // [Required(ErrorMessage = "Enter Album ID")]
    public string Name {get; set;}
    // [Required(ErrorMessage = "Enter Title")]
}

数据库上下文.cs

public class DatabaseContext : DbContext
{
    public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
    {
    }

    public DbSet<Album> albums { get; set; }
    public DbSet<Artist> artists { get; set; }
}

全部专辑.cshtml

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayName("AlbumId")
            </th>
            <th>
                @Html.DisplayName("Title")
            </th>
            <th>
                @Html.DisplayName("ArtistId")
            </th>
            <th>Edit | Delete</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.AlbumList)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.AlbumId)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ArtistId)
                </td>
                <td>
                    <a asp-page="./AllAlbum" asp-route-id="@item.AlbumId">Edit</a> |
                    <a asp-page="./AllAlbum" onclick="return confirm('Are you sure you want to delete this album record?');" asp-page-handler="Delete" asp-route-id="@item.AlbumId">Delete</a>
                </td>
            </tr>
        }
    </tbody>

实体框架应该获取Album ArtistId上的 ArtistId 是Artist表的外键。 那么生成数据的查询可能是:

var albumList = _Context.albums.Include(x => x.artists).ToList();

使用.Include()将生成一个连接表的查询。 对于更详细的查询,您可以像这样生成自己的查询:

var albumsAndArtists = (from albumList in _Context.albums
                       join artistList in _Context.artists
                       on artistList.ArtistId 
                       equals albumList.ArtistId
                       select new 
                       {
                           Album = albumList,
                           Artist = artistList
                       })
                       .ToList();

要将第一个查询与.Include()一起使用,您需要添加:

public virtual Artist artists { get; set; }

Album class 中。 这将启用延迟加载并允许使用.Include()

在您的专辑 class 添加一个外键链接到艺术家:

[ForeignKey("ArtistId")] 
public virtual Artist Artist {get; set;}

然后包括您获取数据的艺术家:

var data = _Context.albums
.Include(x => x.Artist)
.ToList(); 

然后,您可以在您的 cshtml 中使用艺术家数据,例如item.Artist...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM