Stuck on ASP.NET Razor EF core tutorial
I was learning ASP.NET through a Microsoft tutorial, trying to figure out the cause of the data not being created after I clicked on the 'Create New' button to insert new data and I can't even update the data. However, I can delete the data and the seed data.
Been stuck for almost a week now, I have tried to compare the completed code sample given by following this tutorial, https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-8.0&tabs=visual-studio. But the problem is still the same in part 1. Is it normal for the create new button not create any data? Or maybe something I don't understand and see the problem?
If so, can anyone tell me if it's a problem with the database sync, the package or some new error I don't see? Since I was fine doing razor and MVC tutorials without ef core and redoing the tutorial carefully over and over again without any guidance except watching videos online and used ChatGPT(which doesn't help much).
Razor Pages with Entity Framework Core in ASP.NET Core - Tutorial 1...
Shows how to create a Razor Pages app using Entity Framework Core
25 Replies
Impossible to tell what's wrong without seeing your code
I got this error today instead of the page working well yesterday. I did rename every 'Student' error into 'Students' this time.
Wasn't really sure if it's because of the CRUD but here's the code:
Create.cshtml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using ContosoUniversity.Data;
using ContosoUniversity.Models;
namespace ContosoUniversity.Pages.Students
{
public class CreateModel : PageModel
{
private readonly ContosoUniversity.Data.SchoolContext _context;
public CreateModel(ContosoUniversity.Data.SchoolContext context)
{
_context = context;
}
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public Student Student { get; set; } = default!;
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD public async Task<IActionResult> OnPostAsync() { if (!ModelState.IsValid _context.Students == null Student == null) { return Page(); } _context.Students.Add(Student); await _context.SaveChangesAsync(); return RedirectToPage("./Index"); } } }
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD public async Task<IActionResult> OnPostAsync() { if (!ModelState.IsValid _context.Students == null Student == null) { return Page(); } _context.Students.Add(Student); await _context.SaveChangesAsync(); return RedirectToPage("./Index"); } } }
Part 2, Razor Pages with EF Core in ASP.NET Core - CRUD
Part 2 of Razor Pages and Entity Framework tutorial series.
Okay, so you renamed something?
Yep
If you changed anything about the database models, you need to create a new migration and apply it
Also, the code on the error page is not the code you sent
I don't see this anywhere
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using ContosoUniversity.Data;
using ContosoUniversity.Models;
namespace ContosoUniversity.Pages.Students
{
public class IndexModel : PageModel
{
private readonly ContosoUniversity.Data.SchoolContext _context;
public IndexModel(ContosoUniversity.Data.SchoolContext context)
{
_context = context;
}
public IList<Student> Student { get;set; } = default!;
public async Task OnGetAsync()
{
if (_context.Students != null)
{
Student = await _context.Students.ToListAsync();
}
}
}
}
Sorry I sent the wrong code earlier
I'll try this now
Alright, adding the Migration did help make the page work. But the data I inserted into the form and created afterwards. It does not appear.
The output from my Visual Code 2022 come out like this:
Microsoft.EntityFrameworkCore.Infrastructure: Information: Entity Framework Core 6.0.27 initialized 'SchoolContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.27' with options: None
Microsoft.EntityFrameworkCore.Database.Command: Information: Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [s].[ID], [s].[EnrollmentDate], [s].[FirstMidName], [s].[LastName]
FROM [Student] AS [s]
The thread '[Thread Destroyed]' (9400) has exited with code 0 (0x0).
It doesn't create any errors so I'm not sure what is the problem
go through the code/database step by step and find the first point where it goes wrong
it can not throw any exceptions and still be incorrect
Alright, I'll try and check it again
Does this count as one of the problems?
I had already switch the solution to .net 6.0 and had to download the package manually for it
Here are my .net sdks. Should i download an older version for it?
Does putting the command, " Install-Package Microsoft.EntityFrameworkCore.Tools -Version 6.0.0 " helps? because it goes well instead of this
i mean yeah but your code was already running
the problem is in your code, not the packages
you should be using .NET 8 btw, that's the latest version
go through your code with a debugger or at minimum verify the database has the data you expect in it
to find the source of the problem you're going to have to break it down into steps and find which step isn't working
Ahh i see. I'll redo it again just in case
I'll check which ones are the one cause the issues
Ok, it's the same thing with no errors this time, which is concerning for me.
does it got to do with sql in Program.cs?
The database is already in my SSOE and it doesn't seem to be interacting with CRUD Razor page asides Delete
Also tq for being patient with me so far in solving this error :Smadge:
I'm new to ASP.NET so I might be an idiot sometimes :catpls:
right, this is the thing about debugging
your program won't necessarily show you an error if something isn't working, because you made a mistake that doesn't result in an explicit error being thrown
you have at least 2 main things to check here
1. creating a student
2. displaying students
you need to review your code and find out at which point the code stops doing what you intended
is the data actually being stored in the database?
For the Seed Data, Yes
i'm not asking about seed data, i'm asking about the code that you're saying doesn't work
Here is the create student, I'm not sure if it got to do with the code i initially wanted it to work. Does it got to do with ModelState? I remember i used chatgpt for it and it kept stating about ModelState?
Here's the display code
use the debugger to check that all your variables hold the data you think they do at the right time
Alright
Ok. I found the main issue and fix it. It was the Student class in Models folder :catfacepalm:
It doesn't interact with my database because I didn't declare my variable nullable. After I put the string with a ? it works.
Thank you for your suggestion and help :catlurk: