Hi.
I am using SQL and trying to use ASP.NET Core and Razor pages.
The sql table does not have a primary key and I will not be able to make changes to the SQL table. I am trying to use a stored procedure to retrieve the data from the table and show the resultant data in UI.
Since there is no primary key available, I am getting error as - Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys
Below is the code so far used.
//Data - myDbContext public class MyDbContext : DbContext { public DbSet<LOB> lobs { get; set; } public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { } } // Model public class LOB { public string Desc { get; set; } }
//Index.cshtml.cs public class IndexModel : PageModel { private readonly MyDbContext _dbContext; public IndexModel(MyDbContext dbContext) { _dbContext = dbContext; } public List<LOB> lOBs { get; set; } = new List<LOB>(); [BindProperty] public string[] SelectedLOBs { get; set; } public SelectList LOBOptions { get; set; } public async Task OnGetAsync() { lOBs = await _dbContext.Set<LOB>().FromSql("EXECUTE sp") .AsNoTracking() .ToListAsync(); LOBOptions = new SelectList(lOBs, "Desc1"); } } // Index.cshtml <select class="form-control" required multiple id="selLOB" asp-for="SelectedLOBs" asp-items="Model.LOBOptions"></select>
How to fix this?
Thanks