TransWikia.com

A network-related or instance-specific error with C#

Stack Overflow Asked by Nathan Nguyen on December 15, 2020

I get an error like this:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 – Could not open a connection to SQL Server)

Here is my apppsetting.json:

  "DBInfo": 
  {
    "Name": "MySQLconnect",
    "ConnectionString": "server=localhost;user Id=root;password=root;database=cinemapanda4;"
  }

and here is my StartUp.cs:

public class Startup
{
   public Startup(IConfiguration configuration)
   {
      Configuration = configuration;
   }
   public IConfiguration Configuration { get; }

   // This method gets called by the runtime. Use this method to add services to the container.
   public void ConfigureServices(IServiceCollection services)
   {
      services.AddSession();
      services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration["DBInfo:ConnectionString"]));
      services.AddControllersWithViews();
   }

   // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
   {
      app.UseDeveloperExceptionPage();
      app.UseStaticFiles();
      app.UseRouting();
      app.UseSession();
      app.UseAuthorization();
      app.UseEndpoints(endpoints => {
         endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
      });
   }
}

I’m using MacBook and how can I fix this problem?

One Answer

Looks like you're trying to use a MySQL database with EF Core, but connecting with SqlServer driver, which won't work. To connect to MySQL, you can use the Pomelo Driver for EF Core.

Configuration for which can be found here: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#3-services-configuration

Answered by sauravsahu on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP