Adding database configuration #1
26
MainProject/Core/Database/MongoDbContext.cs
Normal file
26
MainProject/Core/Database/MongoDbContext.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.EntityFrameworkCore;
|
||||||
|
using MongoDB.EntityFrameworkCore.Extensions;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Database.Mongo;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Core.Database
|
||||||
|
{
|
||||||
|
public class MongoDbContext : DbContext
|
||||||
|
{
|
||||||
|
public MongoDbContext(DbContextOptions<MongoDbContext> options) : base(options) { }
|
||||||
|
|
||||||
|
public DbSet<Log> Logs { get; set; }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
modelBuilder.Entity<Log>().ToCollection("Logs");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
20
MainProject/Core/Database/SqlServerContext.cs
Normal file
20
MainProject/Core/Database/SqlServerContext.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Core.Database
|
||||||
|
{
|
||||||
|
public class SqlServerContext : DbContext
|
||||||
|
{
|
||||||
|
|
||||||
|
public SqlServerContext(DbContextOptions<SqlServerContext> options)
|
||||||
|
: base(options)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public DbSet<User> Users { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -13,9 +13,24 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
|
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.8" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||||
|
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
|
||||||
|
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="8.1.0" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="NLog" Version="5.2.8" />
|
<PackageReference Include="NLog" Version="5.2.8" />
|
||||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
|
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
|
||||||
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="8.0.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="8.0.0" />
|
||||||
|
|||||||
11
MainProject/Models/Database/Mongo/Log.cs
Normal file
11
MainProject/Models/Database/Mongo/Log.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.Mongo
|
||||||
|
{
|
||||||
|
public class Log
|
||||||
|
{
|
||||||
|
[BsonId]
|
||||||
|
public ObjectId Id { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
16
MainProject/Models/Database/SqlServer/Base.cs
Normal file
16
MainProject/Models/Database/SqlServer/Base.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||||
|
{
|
||||||
|
public class Base
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public DateTime CreationTime { get; set; }
|
||||||
|
public int CreationUserId { get; set; }
|
||||||
|
public DateTime UpdateTime { get; set; }
|
||||||
|
public int UpdateUserId { get; set; }
|
||||||
|
public DateTime DeletionTime { get; set; }
|
||||||
|
public int DeletionUserId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
12
MainProject/Models/Database/SqlServer/Role.cs
Normal file
12
MainProject/Models/Database/SqlServer/Role.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||||
|
{
|
||||||
|
public class Role : Base
|
||||||
|
{
|
||||||
|
public required string Name { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
19
MainProject/Models/Database/SqlServer/User.cs
Normal file
19
MainProject/Models/Database/SqlServer/User.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||||
|
{
|
||||||
|
public class User : Base
|
||||||
|
{
|
||||||
|
public required string Username { get; set; }
|
||||||
|
public required string FirstName { get; set; }
|
||||||
|
public required string LastName { get; set; }
|
||||||
|
public required string Email { get; set; }
|
||||||
|
public required Role Role { get; set; }
|
||||||
|
|
||||||
|
[JsonIgnore]
|
||||||
|
public required string PasswordHash { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -6,6 +6,7 @@ public class AppSettings
|
|||||||
public Settings? Settings { get; set; }
|
public Settings? Settings { get; set; }
|
||||||
public PrivateSettings? PrivateSettings { get; set; }
|
public PrivateSettings? PrivateSettings { get; set; }
|
||||||
public OpenApiSettings? OpenApiSettings { get; set; }
|
public OpenApiSettings? OpenApiSettings { get; set; }
|
||||||
|
public DatabaseSettings? DatabaseSettings { get; set; }
|
||||||
|
|
||||||
#nullable disable
|
#nullable disable
|
||||||
}
|
}
|
||||||
9
MainProject/Models/Settings/DatabaseSettings.cs
Normal file
9
MainProject/Models/Settings/DatabaseSettings.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
|
||||||
|
public class DatabaseSettings
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? SqlServerConnectionString { get; set; }
|
||||||
|
public MongoDbSettings? MongoDbSettings { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
9
MainProject/Models/Settings/MongoDbSettings.cs
Normal file
9
MainProject/Models/Settings/MongoDbSettings.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
|
||||||
|
public class MongoDbSettings
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? MongoDbConnectionString { get; set; }
|
||||||
|
public string? DatabaseName { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
using System;
|
using NLog;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using Microsoft.OpenApi.Models;
|
|
||||||
using NLog;
|
|
||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using BasicDotnetTemplate.MainProject.Utils;
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject;
|
namespace BasicDotnetTemplate.MainProject;
|
||||||
|
|
||||||
public static class ReflectionProgram
|
public static class ReflectionProgram
|
||||||
@@ -41,8 +40,10 @@ internal static class Program
|
|||||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder);
|
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder);
|
||||||
ProgramUtils.AddServices(ref builder);
|
ProgramUtils.AddServices(ref builder);
|
||||||
ProgramUtils.AddOpenApi(ref builder, appSettings);
|
ProgramUtils.AddOpenApi(ref builder, appSettings);
|
||||||
|
ProgramUtils.AddDbContext(ref builder, appSettings);
|
||||||
WebApplication app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
ProgramUtils.AddMiddlewares(ref app);
|
ProgramUtils.AddMiddlewares(ref app);
|
||||||
|
|
||||||
Logger.Info("[Program][Initialize] End building");
|
Logger.Info("[Program][Initialize] End building");
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
using System;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
using MongoDB.Driver;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Utils;
|
namespace BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
|
||||||
@@ -125,4 +127,53 @@ public static class ProgramUtils
|
|||||||
Logger.Info("[ProgramUtils][AddMiddlewares] Done middlewares");
|
Logger.Info("[ProgramUtils][AddMiddlewares] Done middlewares");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void AddDbContext(ref WebApplicationBuilder builder, AppSettings appSettings)
|
||||||
|
{
|
||||||
|
Logger.Info("[ProgramUtils][AddDbContext] Adding DbContext");
|
||||||
|
var databaseAdded = "";
|
||||||
|
|
||||||
|
var connectionString = appSettings?.DatabaseSettings?.SqlServerConnectionString ?? String.Empty;
|
||||||
|
|
||||||
|
if (!String.IsNullOrEmpty(connectionString))
|
||||||
|
{
|
||||||
|
connectionString = connectionString.Replace("SQLSERVER_DB_SERVER", Environment.GetEnvironmentVariable("SQLSERVER_DB_SERVER"));
|
||||||
|
connectionString = connectionString.Replace("SQLSERVER_DB_DATABASE", Environment.GetEnvironmentVariable("SQLSERVER_DB_DATABASE"));
|
||||||
|
connectionString = connectionString.Replace("SQLSERVER_DB_USER", Environment.GetEnvironmentVariable("SQLSERVER_DB_USER"));
|
||||||
|
connectionString = connectionString.Replace("SQLSERVER_DB_PASSWORD", Environment.GetEnvironmentVariable("SQLSERVER_DB_PASSWORD"));
|
||||||
|
|
||||||
|
builder.Services.AddDbContext<SqlServerContext>(options =>
|
||||||
|
options.UseSqlServer(connectionString));
|
||||||
|
|
||||||
|
databaseAdded += "SqlServer";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
connectionString = appSettings?.DatabaseSettings?.MongoDbSettings?.MongoDbConnectionString ?? String.Empty;
|
||||||
|
|
||||||
|
if (!String.IsNullOrEmpty(connectionString))
|
||||||
|
{
|
||||||
|
connectionString = connectionString.Replace("MONGODB_DB_SERVER", Environment.GetEnvironmentVariable("MONGODB_DB_SERVER"));
|
||||||
|
connectionString = connectionString.Replace("MONGODB_DB_DATABASE", Environment.GetEnvironmentVariable("MONGODB_DB_DATABASE"));
|
||||||
|
connectionString = connectionString.Replace("MONGODB_DB_USER", Environment.GetEnvironmentVariable("MONGODB_DB_USER"));
|
||||||
|
connectionString = connectionString.Replace("MONGODB_DB_PASSWORD", Environment.GetEnvironmentVariable("MONGODB_DB_PASSWORD"));
|
||||||
|
|
||||||
|
var mongoClient = new MongoClient(connectionString);
|
||||||
|
|
||||||
|
var databaseName = appSettings?.DatabaseSettings?.MongoDbSettings?.DatabaseName ?? Environment.GetEnvironmentVariable("MONGODB_DB_DATABASE") ?? String.Empty;
|
||||||
|
|
||||||
|
if (!String.IsNullOrEmpty(databaseName))
|
||||||
|
{
|
||||||
|
var dbContextOptions = new DbContextOptionsBuilder<MongoDbContext>()
|
||||||
|
.UseMongoDB(mongoClient, databaseName);
|
||||||
|
}
|
||||||
|
|
||||||
|
databaseAdded += (String.IsNullOrEmpty(databaseAdded) ? "" : ", ") + "MongoDB";
|
||||||
|
}
|
||||||
|
|
||||||
|
var message = String.IsNullOrEmpty(databaseAdded) ? "No DbContext added" : $"{databaseAdded} added";
|
||||||
|
|
||||||
|
Logger.Info($"[ProgramUtils][AddDbContext] {message}");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,13 @@
|
|||||||
"Version": "v1.0",
|
"Version": "v1.0",
|
||||||
"Description": "This template contains basic configuration for a .Net 8 backend"
|
"Description": "This template contains basic configuration for a .Net 8 backend"
|
||||||
},
|
},
|
||||||
|
"DatabaseSettings": {
|
||||||
|
"SqlServerConnectionString": "Server=SQLSERVER_DB_SERVER;Initial Catalog=SQLSERVER_DB_DATABASE;User Id=SQLSERVER_DB_USER;Password=SQLSERVER_DB_PASSWORD;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;",
|
||||||
|
"MongoDbSettings": {
|
||||||
|
"MongoDbConnectionString": "mongodb://MONGODB_DB_USER:MONGODB_DB_PASSWORD@MONGODB_DB_SERVER:27017/MONGODB_DB_DATABASE",
|
||||||
|
"DatabaseName": "BaseDb"
|
||||||
|
}
|
||||||
|
},
|
||||||
"OpenApiSettings": {
|
"OpenApiSettings": {
|
||||||
"TermsOfServiceUrl": "",
|
"TermsOfServiceUrl": "",
|
||||||
"OpenApiContact": {
|
"OpenApiContact": {
|
||||||
@@ -16,7 +23,12 @@
|
|||||||
"Name": "MIT License",
|
"Name": "MIT License",
|
||||||
"Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md"
|
"Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user