【问题标题】:Razor Pages - Relationship One-To-ManyRazor Pages - 一对多关系
【发布时间】:2021-06-19 16:05:28
【问题描述】:

我试图呈现一对多的关系,但总是出现错误,我无法继续前进。 如果有人能找出错误,非常感谢。 最终目标是在 Villas 表中选择一条记录,并能够关联另一个表中的记录。 谢谢

using Microsoft.EntityFrameworkCore;

namespace LeisureVillas.RazorPages.Models
{
    public class VillaStatAContext : DbContext
    {
        public DbSet<StatementA> Tbl_Statement_Autos { get; set; }
        public DbSet<Villa> Tbl_Villas { get; set; }
        public VillaStatAContext(DbContextOptions<VillaStatAContext> options) : base(options)
        {
        }
        #region Required
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
           modelBuilder.Entity<Villa>()
               .HasMany<StatementA>(s => s.StatementAs)
               .WithOne(p => p.Villas)
               .HasForeignKey(s => s.Stat_A_Villas);
            #endregion
        }
    }
}

using LeisureVillas.RazorPages.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;
using System.Linq;

namespace LeisureVillas.RazorPages.Pages
{
    [Authorize(Roles = "Manager")]
    public class VillaStatementA : PageModel
    {
        private readonly VillaStatAContext db = null;

        public List<Villa> Villas { get; set; }
        public List<StatementA> StatementAs { get; set; }

        public void OnGet()
        {
            this.Villas = (from e in db.Tbl_Villas orderby e.Vill_Name select e).ToList();
        }
    }
}

【问题讨论】:

  • 有什么错误?
  • 此时它出现错误,因为我设置了 db = null 并且我不能从这里开始
  • 我打算把两张表的关系放到cshtml页面
  • 如果你这样做,它不会为空:db = new VillaStatAContext()
  • 您可能希望使用依赖注入来提供VillaStatAContext 作为页面模型构造函数。

标签: c# entity-framework asp.net-core entity-relationship razor-pages


【解决方案1】:

使用注释中提到的依赖注入:

[Authorize(Roles = "Manager")]
public class VillaStatementA : PageModel
{
    private readonly VillaStatAContext _db;

    public VillaStatementA(VillaStatAContext db)
    {
        _db = db;
    }

    public List<Villa> Villas { get; set; }
    public List<StatementA> StatementAs { get; set; }

    public void OnGet()
    {
        this.Villas = (from e in db.Tbl_Villas orderby e.Vill_Name select e).ToList();
    }
}

【讨论】:

  • 目前代码没有任何错误,但是html没有打开。它显示以下错误:“处理此请求时出现意外错误。”
  • 切换到开发环境显示详细信息。
【解决方案2】:
@page
@model VillaStatementA
<h3>Suppliers Automatic</h3>
<div class="flex-cont">
    <div class="flex-item-left">
        <font size="2" face="Courier New">
            <table>
                <tr>
                    <th style="display:none;" bgcolor="#5D7B9D"><font color="#fff">ID</font></th>
                    <th bgcolor="#5D7B9D"><font color="#fff">Code</font></th>
                    <th bgcolor="#5D7B9D"><font color="#fff">Customer Name</font></th>
                    <th bgcolor="#5D7B9D"><font color="#fff">Address</font></th>
                    <th bgcolor="#5D7B9D"><font color="#fff">Location</font></th>
                </tr>
                @foreach (var item in Model.Villas)
                {
                    <tr>
                        <td style="display:none;">@item.Vill_ID </td>
                        <td>@item.Vill_Code</td>
                        <td>@item.Vill_Name</td>
                        <td>@item.Vill_Address</td>
                        <td>@item.Vill_Location</td>
                    </tr>
                }
            </table>
        </font>
    </div>
    <div class="flex-item-right">2</div>
    <font size="2" face="Courier New">
        <table>
            <tr>
                <th style="display:none;" bgcolor="#5D7B9D"><font color="#fff">ID</font></th>
                <th bgcolor="#5D7B9D"><font color="#fff">Villa</font></th>
                <th bgcolor="#5D7B9D"><font color="#fff">Amount Débit</font></th>
               <th bgcolor="#5D7B9D"><font color="#fff">Memo</font></th>
            </tr>
            @foreach (var item in Model.StatementAs)
            {
                <tr>
                    <td style="display:none;">@item.Stat_A_Id</td>
                    <td>@item.Stat_A_Villas</td>
                    <td>@item.Stat_A_Amount_Debt</td>
                    <td>@item.Stat_A_Memo</td>
                </tr>
            }
        </table>
    </font>
</div>

【讨论】:

  • 目前代码没有任何错误,但是html没有打开。它显示以下错误:“处理此请求时出现意外错误。”
  • 您应该调试以查找错误详细信息。
  • 我什么都看不见了!! 6小时后我必须起床去上班。非常感谢您的帮助。
猜你喜欢
  • 2018-06-29
  • 2018-09-08
  • 2018-06-15
  • 2021-12-30
  • 2018-09-27
  • 2011-03-30
  • 2015-09-20
  • 2018-03-16
  • 2021-01-29
相关资源
最近更新 更多