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

Convert Array to string in Java/Groovy

5  Asked on January 26, 2021 by maaz

     

List of tuples remove duplicates by first index

3  Asked on January 26, 2021 by rankinstudio

   

Joining based on Sum of amount

1  Asked on January 26, 2021 by rohit-pandey

   

ARKit and Recording / Replaying Image+Depth with time lapse

0  Asked on January 26, 2021 by bsharer-app-share-books

   

Recognizing EOF vs newline in getline in C

3  Asked on January 26, 2021 by jan-dunder

       

How to Find Connected Components in dataset?

0  Asked on January 26, 2021 by mohamed-mohsen

       

Can’t get responses for intents in dialogflow using custom payloads

0  Asked on January 26, 2021 by luis-ramon-ramirez-rodriguez

   

How to initialize correctly an array with Array.CreateInstance() method

1  Asked on January 26, 2021 by donmiguelsan

 

Combine two list of map java 8

1  Asked on January 25, 2021 by jeeppp

     

Ask a Question

Get help from others!

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