#5 Added sqlite as default db provider

This commit is contained in:
amdnaji 2024-05-06 06:54:49 +00:00
parent 4aec2938f9
commit b1f7fa53f8
13 changed files with 187 additions and 47 deletions

View File

@ -22,6 +22,7 @@
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,33 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Manarah.App.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Server",
columns: table => new
{
IPAddress = table.Column<string>(type: "nvarchar(450)", nullable: false),
Description = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Server", x => x.IPAddress);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Server");
}
}
}

View File

@ -10,12 +10,25 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ManarahContext>(options => var configuration = builder.Configuration;
//Set db to Sqlite if as default db provider
var provider = configuration.GetValue("Provider", "Sqlite");
builder.Services.AddDbContext<ManarahContext>(
options => _ = provider switch
{ {
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")); "Sqlite" => options.UseSqlite(
configuration.GetConnectionString("SqliteConnection"),
x => x.MigrationsAssembly("Manarah.SqliteMigrations")),
"SqlServer" => options.UseSqlServer(
configuration.GetConnectionString("SqlServerConnection"),
x => x.MigrationsAssembly("Manarah.SqlServerMigrations")),
_ => throw new Exception($"Unsupported provider: {provider}")
}); });
//Register Service of typ Base Service for DI Container //Register Service of typ Base Service for DI
var assembly = Assembly.Load(typeof(BaseService).Assembly.FullName); var assembly = Assembly.Load(typeof(BaseService).Assembly.FullName);
var serviceList = assembly.DefinedTypes.Where(d => d.BaseType == typeof(BaseService)); var serviceList = assembly.DefinedTypes.Where(d => d.BaseType == typeof(BaseService));
foreach (var service in serviceList) foreach (var service in serviceList)
@ -33,7 +46,7 @@ if (!app.Environment.IsDevelopment())
app.UseHsts(); app.UseHsts();
} }
app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();

View File

@ -1,8 +1,9 @@
{ {
"ConnectionStrings": { "ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=ManarahDb;Trusted_Connection=True;MultipleActiveResultSets=true;encrypt=false" "SqlServerConnection": "Server=localhost;Database=ManarahDb;Trusted_Connection=True;MultipleActiveResultSets=true;encrypt=false",
"SqliteConnection": "Data Source=Manarah.db;Cache=Shared"
}, },
"Provider": "Sqlite",
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>..\Manarah.App\bin\</BaseOutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Manarah.App\Manarah.App.csproj" />
</ItemGroup>
</Project>

View File

@ -8,11 +8,11 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable #nullable disable
namespace Manarah.App.Migrations namespace Manarah.SqlServerMigrations.Migrations
{ {
[DbContext(typeof(ManarahContext))] [DbContext(typeof(ManarahContext))]
[Migration("20240327205653_InitialCreate")] [Migration("20240505211647_InitCreate")]
partial class InitialCreate partial class InitCreate
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)

View File

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Manarah.SqlServerMigrations.Migrations
{
/// <inheritdoc />
public partial class InitCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable #nullable disable
namespace Manarah.App.Migrations namespace Manarah.SqlServerMigrations.Migrations
{ {
[DbContext(typeof(ManarahContext))] [DbContext(typeof(ManarahContext))]
partial class ManarahContextModelSnapshot : ModelSnapshot partial class ManarahContextModelSnapshot : ModelSnapshot

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>..\Manarah.App\bin\</BaseOutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Manarah.App\Manarah.App.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,37 @@
// <auto-generated />
using Manarah.App.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Manarah.SqliteMigrations.Migrations
{
[DbContext(typeof(ManarahContext))]
[Migration("20240505211518_InitCreate")]
partial class InitCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.3");
modelBuilder.Entity("Manarah.Domain.Server", b =>
{
b.Property<string>("IPAddress")
.HasColumnType("TEXT");
b.Property<string>("Description")
.HasColumnType("TEXT");
b.HasKey("IPAddress");
b.ToTable("Server", (string)null);
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Manarah.SqliteMigrations.Migrations
{
/// <inheritdoc />
public partial class InitCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View File

@ -0,0 +1,34 @@
// <auto-generated />
using Manarah.App.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Manarah.SqliteMigrations.Migrations
{
[DbContext(typeof(ManarahContext))]
partial class ManarahContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.3");
modelBuilder.Entity("Manarah.Domain.Server", b =>
{
b.Property<string>("IPAddress")
.HasColumnType("TEXT");
b.Property<string>("Description")
.HasColumnType("TEXT");
b.HasKey("IPAddress");
b.ToTable("Server", (string)null);
});
#pragma warning restore 612, 618
}
}
}

View File

@ -3,9 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.8.34525.116 VisualStudioVersion = 17.8.34525.116
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Manarah.App", "Manarah.App\Manarah.App.csproj", "{C505E413-CB8E-49A6-9186-AFDED3B878BD}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Manarah.App", "Manarah.App\Manarah.App.csproj", "{C505E413-CB8E-49A6-9186-AFDED3B878BD}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Manarah.Domain", "Manarah.Domain\Manarah.Domain.csproj", "{1551179E-FA6B-4E87-8B50-9FA2AD2F0CB0}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Manarah.Domain", "Manarah.Domain\Manarah.Domain.csproj", "{1551179E-FA6B-4E87-8B50-9FA2AD2F0CB0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Database", "Database", "{D07B611D-55F9-4830-949C-850677ED48EB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Manarah.SqliteMigrations", "Manarah.SqliteMigrations\Manarah.SqliteMigrations.csproj", "{794BD3E8-AD7B-4C57-95E1-78CD0AA03DB8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Manarah.SqlServerMigrations", "Manarah.SqlServerMigrations\Manarah.SqlServerMigrations.csproj", "{DF9FC8BA-0854-465E-9C84-9FC330120527}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -21,10 +27,22 @@ Global
{1551179E-FA6B-4E87-8B50-9FA2AD2F0CB0}.Debug|Any CPU.Build.0 = Debug|Any CPU {1551179E-FA6B-4E87-8B50-9FA2AD2F0CB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1551179E-FA6B-4E87-8B50-9FA2AD2F0CB0}.Release|Any CPU.ActiveCfg = Release|Any CPU {1551179E-FA6B-4E87-8B50-9FA2AD2F0CB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1551179E-FA6B-4E87-8B50-9FA2AD2F0CB0}.Release|Any CPU.Build.0 = Release|Any CPU {1551179E-FA6B-4E87-8B50-9FA2AD2F0CB0}.Release|Any CPU.Build.0 = Release|Any CPU
{794BD3E8-AD7B-4C57-95E1-78CD0AA03DB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{794BD3E8-AD7B-4C57-95E1-78CD0AA03DB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{794BD3E8-AD7B-4C57-95E1-78CD0AA03DB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{794BD3E8-AD7B-4C57-95E1-78CD0AA03DB8}.Release|Any CPU.Build.0 = Release|Any CPU
{DF9FC8BA-0854-465E-9C84-9FC330120527}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF9FC8BA-0854-465E-9C84-9FC330120527}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF9FC8BA-0854-465E-9C84-9FC330120527}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF9FC8BA-0854-465E-9C84-9FC330120527}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{794BD3E8-AD7B-4C57-95E1-78CD0AA03DB8} = {D07B611D-55F9-4830-949C-850677ED48EB}
{DF9FC8BA-0854-465E-9C84-9FC330120527} = {D07B611D-55F9-4830-949C-850677ED48EB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A3C4374E-F704-4D7C-88D0-BBEB231C39FC} SolutionGuid = {A3C4374E-F704-4D7C-88D0-BBEB231C39FC}
EndGlobalSection EndGlobalSection