Add project files.
This commit is contained in:
48
StockDBEF/GreetingService.cs
Normal file
48
StockDBEF/GreetingService.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StockDBEF.Models;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
// DI, SeriLog, Settings
|
||||
|
||||
namespace StockDBEF
|
||||
{
|
||||
public class GreetingService : IGreetingService
|
||||
{
|
||||
private readonly ILogger<GreetingService> _log;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly StockDBContext _stockDBContext;
|
||||
|
||||
public GreetingService(ILogger<GreetingService> log, IConfiguration config, StockDBContext stockDBContext)
|
||||
{
|
||||
_log = log;
|
||||
_config = config;
|
||||
_stockDBContext = stockDBContext;
|
||||
}
|
||||
public async void Run()
|
||||
{
|
||||
for (int i = 0; i < _config.GetValue<int>("LoopTimes"); i++)
|
||||
{
|
||||
_log.LogInformation("Run number {runNumber}", i);
|
||||
}
|
||||
|
||||
await SaveInitialStock();
|
||||
await SaveInitialPerson();
|
||||
}
|
||||
|
||||
public async Task SaveInitialStock()
|
||||
{
|
||||
StockMember sm = new StockMember { BuyValue = 120M, ActAmount = 100, BuyDate = DateTime.Parse("2021-03-01"), PostAmount = 100, Comment = "Initial aktiepost" };
|
||||
_stockDBContext.StockMembers.Add(sm);
|
||||
await _stockDBContext.SaveChangesAsync();
|
||||
}
|
||||
public async Task SaveInitialPerson()
|
||||
{
|
||||
Person p = new Person { FirstName="Nisse", LastName="Pärlemo", Born=DateTime.Parse("1995-10-15"), NickName="Nippe" };
|
||||
_stockDBContext.Persons.Add(p);
|
||||
await _stockDBContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
8
StockDBEF/IGreetingService.cs
Normal file
8
StockDBEF/IGreetingService.cs
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
namespace StockDBEF
|
||||
{
|
||||
public interface IGreetingService
|
||||
{
|
||||
void Run();
|
||||
}
|
||||
}
|
||||
17
StockDBEF/Models/Person.cs
Normal file
17
StockDBEF/Models/Person.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StockDBEF.Models
|
||||
{
|
||||
public class Person
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string NickName { get; set; }
|
||||
public DateTime Born { get; set; }
|
||||
}
|
||||
}
|
||||
28
StockDBEF/Models/StockDBContext.cs
Normal file
28
StockDBEF/Models/StockDBContext.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StockDBEF.Models
|
||||
{
|
||||
public class StockDBContext : DbContext
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public StockDBContext(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
public DbSet<Person> Persons { get; set; }
|
||||
public DbSet<StockMember> StockMembers { get; set; }
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
var connectionString = _configuration.GetValue<string>("ConnectionStrings:Stocks");
|
||||
optionsBuilder.UseSqlite(connectionString);
|
||||
//optionsBuilder.UseSqlite("Data Source=Facts.db");
|
||||
}
|
||||
}
|
||||
}
|
||||
26
StockDBEF/Models/StockMember.cs
Normal file
26
StockDBEF/Models/StockMember.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StockDBEF.Models
|
||||
{
|
||||
public class StockMember
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string StockId { get; set; }
|
||||
public string StockExtId { get; set; }
|
||||
public decimal BuyValue { get; set; }
|
||||
public DateTime BuyDate { get; set; }
|
||||
public decimal ActValue { get; set; }
|
||||
public DateTime ActDate { get; set; }
|
||||
public long ActAmount { get; set; }
|
||||
public decimal SoldValue { get; set; }
|
||||
public DateTime? SoldDate { get; set; }
|
||||
// public string PostId { get; set; }
|
||||
public string Comment { get; set; }
|
||||
public long PostAmount { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
83
StockDBEF/Program.cs
Normal file
83
StockDBEF/Program.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using StockDBEF.Models;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
|
||||
// DI, SeriLog, Settings
|
||||
|
||||
namespace StockDBEF
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var builder = new ConfigurationBuilder();
|
||||
BuildConfig(builder);
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(builder.Build())
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
|
||||
Log.Logger.Information("Application starting");
|
||||
|
||||
var host = Host.CreateDefaultBuilder()
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
services.AddEntityFrameworkSqlite();
|
||||
services.AddHostedService<DatabaseStartup>();
|
||||
//services.AddDbContext<StockDBContext>();
|
||||
services.AddDbContextPool<StockDBContext>(o =>
|
||||
{
|
||||
o.UseSqlite("Data Source=Stocks.db");
|
||||
});
|
||||
|
||||
services.AddTransient<IGreetingService, GreetingService>();
|
||||
})
|
||||
.UseSerilog()
|
||||
.Build();
|
||||
|
||||
var svc = ActivatorUtilities.CreateInstance<GreetingService>(host.Services);
|
||||
svc.Run();
|
||||
}
|
||||
|
||||
static void BuildConfig(IConfigurationBuilder builder)
|
||||
{
|
||||
builder.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
|
||||
.AddEnvironmentVariables();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class DatabaseStartup : IHostedService
|
||||
{
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
public DatabaseStartup(IServiceProvider serviceProvider)
|
||||
{
|
||||
this.serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using (var scope = serviceProvider.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<StockDBContext>();
|
||||
await db.Database.EnsureCreated();
|
||||
// or
|
||||
await db.Database.MigrateAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
}
|
||||
32
StockDBEF/StockDBEF.csproj
Normal file
32
StockDBEF/StockDBEF.csproj
Normal file
@ -0,0 +1,32 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="4.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
15
StockDBEF/appsettings.json
Normal file
15
StockDBEF/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"LoopTimes": 15,
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsft": "Information",
|
||||
"System": "Warning"
|
||||
}
|
||||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"Stocks": "Data Source=Stocks.db"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user