0

I used below connectionstring format for WebApi, it didnot work.

"ConnectionStrings": {
    "DefaultConnection": "Data Source = William\SQL2017Express; Initial Catalog=ContactMgmtDB;Integrated Security=SSPI;"
  },

here the info, How to write a connection string for Database that has "\" for this format Servername\Instanced Name

1)Authentication Method : Windows Authentication
2) Server Name : William\SQL2017EXPRESS
3) Instance Name: SQL2017EXPRESS

Update: This string contain "\\" for escape character. Is this OK
"DefaultConnection": "Data Source = William"\\"SQL2017Express; Initial Catalog=ContactMgmtDB;Integrated Security=SSPI;"

Thanks

MilkBottle
  • 4,242
  • 13
  • 64
  • 146

2 Answers2

0

William - it is your localhost? If no, be sure that named pipes and tcp/ip are enabled on server. Look here for more info

Also your windows user must have access to database. Can you connect to db from SQL Server Management Studio? If not, you should login on server using sa user and add your windows user. More info

Also look at this topic

  • I m confused with the term localhost. Is Server name or Computer Name referring to the Computer itself? Right. If it refer to computer, then my computer name is William. Is my connectionstring correct? – MilkBottle Apr 20 '18 at 10:05
  • try write .\SQL2017Express instead William\SQL2017Express – Artem Kurianov Apr 20 '18 at 10:20
  • Not working. I tried .\SQL2017Express, William\SQL2017Express and I also enabled TCP and name Pipes, I also open port 49xxx for Sqlserver2017. I wonder what are the problems. Please my Update. The string contain the escape character : \ after the word William – MilkBottle Apr 20 '18 at 10:35
  • oh, how i'm inconsiderate. Just use escape symbol '\'. `"ConnectionStrings": { "DefaultConnection": "Data Source = William\\SQL2017Express; Initial Catalog=ContactMgmtDB;Integrated Security=SSPI;" },` – Artem Kurianov Apr 20 '18 at 10:48
  • [Here more info](https://stackoverflow.com/questions/14480724/escape-double-quotes-in-string) about escaping special characters – Artem Kurianov Apr 20 '18 at 10:51
  • I tried everything, it didnot work even the connectionstring is correct. I can send you the app to check. Just give me a temp email. Thanks a million. – MilkBottle Apr 25 '18 at 06:39
  • you want me to post the code here? it is too long or You want me to send you the project. I need your email or created a new email. I have been on this issues for days! I highly suspect that something wrong with the .net core hosting services or the IIS. The connectingstring is correct. The EntityFramework is correctly used. What is the problem that I can not deploy it to local IIS for testing? Thanks – MilkBottle Apr 25 '18 at 11:32
0

You can use also this format, it will be working ASP.NET CORE MVC. First, install these packages in the project:-

  1. Install-Package Microsoft.EntityFrameworkCore -Version 3.1.0
  2. Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.0
  3. Install-Package Microsoft.EntityFrameworkCore.Tools -Version 3.1.0
  4. Install-Package Microsoft.EntityFrameworkCore.Design -Version 3.1.0
  5. Install-Package Microsoft.EntityFrameworkCore.Proxies -Version 3.1.0

then, overwrite this method on your own ContextDb Class:-

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        string connectionString = @"Data Source = William\SQL2017Express; 
                                    Initial Catalog = ContactMgmtDB; 
                                    Integrated Security = SSPI;";
        optionsBuilder.UseSqlServer(connectionString);
        optionsBuilder.UseLazyLoadingProxies();
    }