Razor Pages - 一对多关系
Razor Pages - Relationship One-To-Many
我正在尝试呈现一对多关系,但总是出现错误,无法继续。
如果有人能找出错误,非常感谢。
最后objective是在Villastable中选择一个记录,并且能够关联另一个table中的记录。
谢谢
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();
}
}
}
使用评论中提到的依赖注入:
[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();
}
}
@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>
我正在尝试呈现一对多关系,但总是出现错误,无法继续。 如果有人能找出错误,非常感谢。 最后objective是在Villastable中选择一个记录,并且能够关联另一个table中的记录。 谢谢
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();
}
}
}
使用评论中提到的依赖注入:
[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();
}
}
@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>