I'm new to both Web apps and MySQL, but I'm creating a web app using Razor Pages and I can't figure out how to connect a MySQL database.
All I've found online is either about connecting a Razor pages to SQL or connecting MySQL to a MVC Web app. There was someone asking an almost identical question here 2 years ago, but the only answer given seems to be for connecting MVC to MySQL.
Is there a way to connect a MySQL database to a razor pages web app? Thanks for any help!
Notes: I'm using Visual Studio 2019 and ASP.NET Core 3.1
Yeah, there is no controller class in Razor Pages, we do the logic in the PageModel class. Apart from this, they are almost the same as MVC. You just need to inject the DbContext into the PageModel. Depend on the link you refer, you can change the last part like below:
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IronManContext _context;
public IndexModel(ILogger<IndexModel> logger, IronManContext context)
{
_logger = logger;
_context = context;
}
public void OnGet()
{
var teams = _context.Teams;
}
}