Skip to content

Instantly share code, notes, and snippets.

@jbollman7
Last active December 18, 2021 22:13
Show Gist options
  • Save jbollman7/ae16fd8acc2f6ead3dbb2c7cb9e58490 to your computer and use it in GitHub Desktop.
Save jbollman7/ae16fd8acc2f6ead3dbb2c7cb9e58490 to your computer and use it in GitHub Desktop.
Connecting to Sql server tshooting

Running "dotnet ef migrations add InitialCreate; dotnet ef database update" Will likely fail. Below is some tips on actually connecting

In appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "PizzaDb": "Server=localhost;Database=PizzaDb;User=sa;Password=Password%1#29;Integrated Security=true"
  }
}

You have to add the ConnectionSring part. The database is called PizzaDb and for simplicity i named the connection string PizzaDb but it could have been something else note i make a user account sa, and i se ta password. You must set Integrated Security to true (have no idea what that does) Assuming you're doing minimum apis (.net 6 and newer) go to Program.cs and look at your connection string var builder = WebApplication.CreateBuilder(args); var connectionString = builder.Configuration.GetConnectionString("PizzaDb") ?? "Data Source=Pizzas.db"; //Connection string first req. for using a db. Note howGetConnectionString is looking for PizzaDb, which you have to set in appsettings.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment