1

I am new to webpage development, and trying to connect my ASP.NET Core MVC code to an existing SQL Server database, using SQL Server authentication. Right now it will only take my computer identification, which of course gets rejected by the SQL Server database. Can anyone tell me how I am supposed to do this?

Details:

  • Programing is done in Visual Studio 2019, with ASP.NET Core, Entity Framework. All software is up to date.

  • I followed this tutorial exactly: https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db but it doesn't include SQL Server authentication.

  • I have my SQL Server login information, and it does allow me to manually connect using "Tools -> Connect to Database". I can see my database and navigate through it, but cannot connect to migrate the data over to my MVC files.

  • I did successfully complete the entire Microsoft ASP.NET MVC tutorial wherein you create a movies database and use that for an example webpage. The difficulty comes when connecting to an existing database.

1
  • Thank you guys so much for your help! Commented Jun 9, 2019 at 15:29

2 Answers 2

1

Depending on the Connection method you are using. In your appsettings.json, you can set the connection string like below:

For Windows authentication:

"ConnectionString": "Server=laptop-15;Database=testDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"

For SQL Server authentication:

"ConnectionString": "Server=laptop-15;Database=testDatabase;User Id=user; Password=user;Trusted_Connection=True;MultipleActiveResultSets=true"

Let me know if this solves your issue. Thanks

Sign up to request clarification or add additional context in comments.

Comments

0

In addition to the very good information from Samual Akosile regarding connection strings using SQL Server auth, the simplest way afaik to wire it up uses the built-in GetConnectionString() method.

In appsettings.json add the following (with your appropriate connection string) piece of JSON

  "ConnectionStrings": {
    "SQLServerConnectionStr": "Server=Your connection string goes here;"
  }

Then in startup.cs where you configure the IServiceCollection services add the following

services.AddDbContext<YourDbContextClass>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("SQLServerConnectionStr")));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.