I am trying to present a one-to-many relationship and errors always appear and I cannot move forward. If anyone can identify the errors, thank you very much. The final objective is to choose a record in the Villas table and to be able to associate records in another table. Thanks
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();
}
}
}
Use denpendency injection metioned in comment:
[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();
}
}