I am currently trying to teach myself ASP.NET, as I will need it on the job soon. For testing purpose, I want to create a simple project with 1 or 2 RazorPages, which derives data from a MySQL database on my localhost. I am using the Visual Studio Community version. I started by creating a new ASP.NET Core Web Application project, then added a "New Connection" (see screenshot)
I also installed the packages "MySqlData", "MySqlData.EntityFrameworkCore" and "MySQLConnector". In appsettings.json, I added
"ConnectionStrings": {
"RazorPagesPlayersContext": "server=localhost;user=root;password=password;database=players;"
}
and in Startup.cs, I configured
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddDbContext<RazorPagesPlayersContext>(options => options.UseSqlite(Configuration.GetConnectionString("RazorPagesPlayersContext")));
}
However, when I to go to the create page and post a new "player" into the database, I am getting error
ArgumentException: Connection string keyword 'server' is not supported
My context file looks like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using RazorPagesPlayers.Models;
namespace RazorPagesPlayers.Data
{
public class RazorPagesPlayersContext : DbContext
{
public RazorPagesPlayersContext (DbContextOptions<RazorPagesPlayersContext> options)
: base(options)
{
}
public DbSet<RazorPagesPlayers.Models.Player> Player { get; set; }
}
}
When I check "Connected Services", I can only see this
I can see, that the connection string of this service has nothing to do with the connection string I added in appsettings.json. When I try to change it, it will just switch back. So I am guessing the connection to MySQL failed or am I confusing things here? I am not even sure if I am looking in the right direction, but every hint would be most welcome. Thank you very much in advance
Edit: When I change the Service config in Startup.cs to
services.AddDbContext<RazorPagesPlayersContext>(options => options.UseMySql(Configuration.GetConnectionString("RazorPagesPlayersContext")));
it tells me "DbContextOptionsBuilder" does not contains a defintion for 'UseMySql' and recommends to change it to UseMySQL. This however results in an error:
TypeLoadException: Method 'Create' in type 'MySql.Data.EntityFrameworkCore.Query.Internal.MySQLSqlTranslatingExpressionVisitorFactory' from assembly 'MySql.Data.EntityFrameworkCore, Version=8.0.22.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' does not have an implementation.
is there a point in installing Pomelo?
question from:
https://stackoverflow.com/questions/65917660/visual-studio-connect-mysql-database-running-on-localhost-to-asp-net-project