ConnectionString in appsettings.json: Use in Entity Framework Core for SQL Server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the app settings.json file can be used to store a SQL server connection string this is part two of our app settings.json course and we'll use this connection string in an asp.net core web API to communicate with a SQL Server database through Entity Framework remember to hit the red subscribe button or go to youtube.com round the code to subscribe to our YouTube channel learn.net depends injection and blazer webassembly with round the coats online courses go to aroundthecode.com courses the connection string can be added as part of the app settings.json file to do that we add it as part of the connection strings section we've named our app settings.db context and it just contains our connection string to our SQL Server database this is the database that it's referring to it's called round the code underscore app settings it's got one table in there called authentication and one record of ID client ID client secret and attempts and that's all in the database for us this is entity Frameworks DB context and we've named it appsettings.db context we've set a DB set of Authentication inside authentication it's got the properties that represent the fields in the table in the database we've also added some settings for that authentication so we've said that the ID is a primary key and we've mapped it to the authentication table within SQL Server these are the new get packages that you need to run Entity framework call with SQL Server you need the main Entity framework core you also need the SQL server and the tools package as well we now need to add the DB context as part of the web application Builder instance to do that we call builder.services and we can call the add DB context method it's expecting a generic type so we're going to add the DB context name which is app settings.db context now we need to set some options we need to set the fact that it's using SQL Server and within that we need to set the connection string we're setting the connection string as partly accessing.json so we're going to pull that into the program.cs so we can call build a DOT configuration and there's a method in there called get connection string the get connection string will look inside the connection strings section within that we'll call the name of the connection string which is app settings DB context that is representing this key here we're now going to use the DB context to read a record into an asp.net core web API we've got our home controller first thing we're going to do is we're going to set a private read-only member we're going to give it a type of app settings DB context and name it as underscore context next thing is to create the Constructor now the DB context is added as pass for the ioc container but dependency injection so we can pass that in as a parameter and then we're just going to set the private member to the parameter that we're passing in next we're going to read the record from the DB context and output it as part of the response for this index action I'm going to modify this slightly so we're going to set it as an async method and for that we need to return a task with a type of Authentication we need to bring in the type for the Authentication we're going to change the response so we're expecting a response so we call the underscore context which is the app settings DB context up here the authentication so we go back into the DB context that's this DB set that is set here and we're going to call the first async method and we're going to get the record where the ID is one run in the application we can see that it's output in the record that we've got from the SQL Server database in the next part of our app settings.json course we'll have a look at how to set up different app settings.json files for different environments such as staging and production check out the full course playlist at runthecode.com app settings hyphen course
Info
Channel: Round The Code
Views: 7,414
Rating: undefined out of 5
Keywords: appsettings connection string, dotnet core appsettings connection string, asp.net core appsettings connection string from controller, appsettings connection string c#, Entity Framework, Entity Framework C#, entity framework connection string, connection string sql server, connection string sql server c#
Id: p4ikc_6zano
Channel Id: undefined
Length: 4min 59sec (299 seconds)
Published: Sun Oct 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.