AnswerBun.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!

Related Questions

String concatenation attempt causing unexpected error

4  Asked on December 4, 2021 by q-q

 

Merging DataFrames with “uneven” data

3  Asked on December 4, 2021 by ribit8950

   

Printing only the value of a key in a dictionary

3  Asked on December 3, 2021 by mr-mac

 

Javascript – Removing last 3 words from a string

4  Asked on December 3, 2021 by maimok

   

Pandas – handle inf and NaN in one line

2  Asked on December 3, 2021 by 8-bit-borges

   

Javascript onclick action not working on image or icon

1  Asked on December 3, 2021 by carrey

   

What does $p[‘t] mean in this keylogger code?

0  Asked on December 3, 2021 by rickyk95

         

Modelsim signal declaration issue

1  Asked on December 3, 2021 by norick

     

sort object properties and JSON.stringify

24  Asked on December 3, 2021 by innovine

   

Ask a Question

Get help from others!

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