From e7c1fde35e04eeaba51c5f6caa8af8420f88470f Mon Sep 17 00:00:00 2001 From: Tim Cliff Date: Fri, 3 Jun 2022 11:37:35 +0100 Subject: [PATCH] Add Database Connection to Installer file --- .editorconfig | 4 ++++ Data/DiaryDBContext.cs | 3 +-- Diary.sln | 5 +++++ Installers/DatabaseInstaller.cs | 17 +++++++++++++++++ Program.cs | 16 +++++----------- 5 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 .editorconfig create mode 100644 Installers/DatabaseInstaller.cs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8d2a1fc --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CS8618: Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. +dotnet_diagnostic.CS8618.severity = none diff --git a/Data/DiaryDBContext.cs b/Data/DiaryDBContext.cs index a62d688..eb9bce9 100644 --- a/Data/DiaryDBContext.cs +++ b/Data/DiaryDBContext.cs @@ -13,8 +13,7 @@ namespace Diary.Data { public DbSet Entries { get; set; } - public DiaryDBContext(DbContextOptions options) : base(options) - { + public DiaryDBContext(DbContextOptions options) : base(options){ } protected override void OnModelCreating(ModelBuilder builder) diff --git a/Diary.sln b/Diary.sln index c57d2a2..b5fc467 100644 --- a/Diary.sln +++ b/Diary.sln @@ -5,6 +5,11 @@ VisualStudioVersion = 17.0.31825.309 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Diary", "Diary.csproj", "{AC078C62-6705-45A9-A00A-7EDDCF770525}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A852FE41-7660-406A-A6F6-8EDE1F7C1586}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/Installers/DatabaseInstaller.cs b/Installers/DatabaseInstaller.cs new file mode 100644 index 0000000..4dc0854 --- /dev/null +++ b/Installers/DatabaseInstaller.cs @@ -0,0 +1,17 @@ +using Diary.Data; +using Microsoft.EntityFrameworkCore; + +namespace Diary.Installers +{ + public static class DatabaseInstaller + { + public static IServiceCollection AddDatabase(this IServiceCollection services, IConfigurationRoot config) + { + return services.AddDbContext(opt => + { + string connectionString = config.GetConnectionString("Diary"); + opt.UseSqlServer(connectionString); + }); + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 17addf3..aad0036 100644 --- a/Program.cs +++ b/Program.cs @@ -1,31 +1,25 @@ using Diary.Data; +using Diary.Installers; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; var builder = WebApplication.CreateBuilder(args); //GetConfigStuff -IConfigurationRoot? config = new ConfigurationBuilder() +IConfigurationRoot config = new ConfigurationBuilder() .AddJsonFile("appsettings.json",false) .Build(); - - - -builder.Services - .AddDbContext(opt => - { - string connectionString = config.GetConnectionString("Diary"); - opt.UseSqlServer(connectionString); - }); +builder.Services.AddDatabase(config); // Add services to the container. -builder.Services.AddDbContext(); + builder.Services.AddControllers(); builder.Services.AddScoped(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen();