Connection string stored in appsettingsjson so open

This preview shows page 38 - 43 out of 66 pages.

Connection String stored in appsettings.json So open up your appsettings.json and add your server name, database name { "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*", "ConnectionString": { "MyBookStoreConnectionStr": "Server= LAPTOP- 2RVGCF6I\\MSSQLSERVER01 ;Database=BookStore;Trusted_Connection=True;Mul tipleActiveResultSets=true" } }
Configuring the Application Now we need to open up our Startup.cs again and add these configurations so it can connect to the database Check the Book object in the database to see if there is any data. using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; namespace MyBookStore {public class Startup { public Startup(IConfiguration configuration) => Configuration = configuration; public IConfiguration Configuration { get; } public Startup(IWebHostingEnvironment env){ Configuration = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json").Build();} public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options =>options.UseSqlServer( Configuration["ConnectionString:MyBookStoreConnectionStr"])); services.AddTransient<IBookRepository, EFBookRepository>(); //services.AddTransient<IBookRepository, FakeBookRepository>(); services.AddMvc(); }
public void Configure(IApplicationBuilder app, IWebHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseStatusCodePages(); app.UseStaticFiles(); app.UseEndpoints(endpoints =>{ endpoints.MapControllerRoute("default", "{controller=Book}/{action=List}/{id?}"); }); } }
Configuring the Application . . . public Startup(IConfiguration configuration) => Configuration = configuration; public IConfiguration Configuration { get; } . . . The Startup constructor loads the configuration settings in the appsettings.json file and makes them available through a property called Configuration. . . . services.AddDbContext<ApplicationDbContext>(options =>options.UseSqlServer( Configuration["ConnectionString:MyBookStoreConnectionStr"])); . . . The AddDbContext extension method sets up the services provided by Entity Framework Core for the database context class. The argument to the AddDbContext method is a lambda expression that receives an options object that configures the database for the context class. We configured the database with the UseSqlServer method and specified the connection string, which is obtained from the Configuration property.
Dependency Injection What is it? Dependency injection makes it easy to create loosely coupled components , which typically means that components consume functionality defined by interfaces without having any first-hand knowledge of which implementation classes are being used. Why is it useful? Dependency injection makes it easier to change the behavior of an application by changing the components that implement the interfaces that define application features. It also results in components that are easier to isolate for unit testing.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture