Fungerande version av skiktad lösning Winforms App med SQlite och entity framework
This commit is contained in:
@ -1,26 +1,44 @@
|
||||
using DIDemoLib;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WinFormDiApp.DAL;
|
||||
|
||||
namespace WinFormDi
|
||||
{
|
||||
public static class ContainerConfig
|
||||
{
|
||||
public static IHost? Configure()
|
||||
|
||||
public static IHost? Configure(IHostBuilder hostBuilder)
|
||||
{
|
||||
var builder = new HostBuilder()
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddEnvironmentVariables()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build();
|
||||
|
||||
|
||||
var builder = hostBuilder // new HostBuilder()
|
||||
.ConfigureServices((_, services) =>
|
||||
{
|
||||
var conn = configuration.GetConnectionString("DatabaseConnection");
|
||||
services
|
||||
.AddDbContext<ApplicationDbContext>(options =>
|
||||
options.UseSqlite(conn))
|
||||
.AddTransient<IMessages, Messages>()
|
||||
.AddTransient<MainWindow>();
|
||||
});
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
BIN
WinFormDi/Local.db
Normal file
BIN
WinFormDi/Local.db
Normal file
Binary file not shown.
@ -1,6 +1,8 @@
|
||||
using DIDemoLib;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using WinFormDiApp.DAL;
|
||||
using WinFormDiApp.DAL.Data;
|
||||
|
||||
namespace WinFormDi
|
||||
{
|
||||
@ -10,15 +12,19 @@ namespace WinFormDi
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var host = ContainerConfig.Configure();
|
||||
var host = ContainerConfig.Configure(CreateHostBuilder(args));
|
||||
|
||||
using var scope = host.Services.CreateScope();
|
||||
|
||||
try
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
|
||||
var context = services.GetRequiredService<ApplicationDbContext>();
|
||||
DataSeeder.Initialize(context);
|
||||
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.SetHighDpiMode(HighDpiMode.SystemAware);
|
||||
@ -32,5 +38,11 @@ namespace WinFormDi
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -9,12 +9,29 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DIDemoLib\DIDemoLib.csproj" />
|
||||
<ProjectReference Include="..\WinFormDiApp.BL\WinFormDiApp.BL.csproj" />
|
||||
<ProjectReference Include="..\WinFormDiApp.DAL\WinFormDiApp.DAL.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Local.db">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
11
WinFormDi/appsettings.json
Normal file
11
WinFormDi/appsettings.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DatabaseConnection": "Data Source=.\\Local.db"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default" : "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user