Fungerande version av skiktad lösning Winforms App med SQlite och entity framework
This commit is contained in:
30
WinFormDiApp.DAL/ApplicationDbContext.cs
Normal file
30
WinFormDiApp.DAL/ApplicationDbContext.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using WinFormDiApp.BL.Models;
|
||||
|
||||
namespace WinFormDiApp.DAL
|
||||
{
|
||||
public class ApplicationDbContext : DbContext
|
||||
{
|
||||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
|
||||
|
||||
public virtual DbSet<Member> Members { get; set; }
|
||||
public virtual DbSet<AccountRecord> AccountRecords { get; set; }
|
||||
}
|
||||
|
||||
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
|
||||
{
|
||||
public ApplicationDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile(@Directory.GetCurrentDirectory() + "/../WinFormDi/appsettings.json")
|
||||
.Build();
|
||||
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
||||
var connectionString = configuration.GetConnectionString("DatabaseConnection");
|
||||
builder.UseSqlite(connectionString);
|
||||
return new ApplicationDbContext(builder.Options);
|
||||
}
|
||||
}
|
||||
}
|
||||
38
WinFormDiApp.DAL/Data/DataSeeder.cs
Normal file
38
WinFormDiApp.DAL/Data/DataSeeder.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WinFormDiApp.BL.Models;
|
||||
|
||||
namespace WinFormDiApp.DAL.Data
|
||||
{
|
||||
public class DataSeeder
|
||||
{
|
||||
public static void Initialize(ApplicationDbContext context)
|
||||
{
|
||||
if (!context.Members.Any())
|
||||
{
|
||||
var members = new List<Member>()
|
||||
{
|
||||
new Member { /*Id = 1,*/ FirstName = "John", LastName="Doe", NickName="Doey", PersonType="Normal", Email = "john@john.com" },
|
||||
new Member { /*Id = 2,*/ FirstName = "Michael", LastName="Jordan", NickName="Joardie", PersonType="Normal", Email = "michael@michael.com" }
|
||||
};
|
||||
context.Members.AddRange(members);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
if (!context.AccountRecords.Any())
|
||||
{
|
||||
var accountRecords = new List<AccountRecord>()
|
||||
{
|
||||
new AccountRecord { /* Id = 1 */ Avisering = "EInvoice", Belopp=10.0, BetalDatum=DateTime.Parse("2023-05-05"), Konto="BG 5787-1030", Mottagare="Kalles Bärplockning" },
|
||||
new AccountRecord { /* Id = 1 */ Avisering = "EInvoice", Belopp = 13.0, BetalDatum = DateTime.Parse("2023-05-07"), Konto = "PG 4158502-7", Mottagare = "Nisses Strumpor" }
|
||||
};
|
||||
context.AccountRecords.AddRange(accountRecords);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
85
WinFormDiApp.DAL/Migrations/20230822204611_Initial.Designer.cs
generated
Normal file
85
WinFormDiApp.DAL/Migrations/20230822204611_Initial.Designer.cs
generated
Normal file
@ -0,0 +1,85 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using WinFormDiApp.DAL;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace WinFormDiApp.DAL.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20230822204611_Initial")]
|
||||
partial class Initial
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "7.0.10");
|
||||
|
||||
modelBuilder.Entity("WinFormDiApp.BL.Models.AccountRecord", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Avisering")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<double>("Belopp")
|
||||
.HasColumnType("REAL");
|
||||
|
||||
b.Property<DateTime>("BetalDatum")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Konto")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Mottagare")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("AccountRecords");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("WinFormDiApp.BL.Models.Member", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("FirstName")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("LastName")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("NickName")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("PersonType")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Members");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
59
WinFormDiApp.DAL/Migrations/20230822204611_Initial.cs
Normal file
59
WinFormDiApp.DAL/Migrations/20230822204611_Initial.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace WinFormDiApp.DAL.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Initial : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "AccountRecords",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
BetalDatum = table.Column<DateTime>(type: "TEXT", nullable: false),
|
||||
Mottagare = table.Column<string>(type: "TEXT", nullable: false),
|
||||
Konto = table.Column<string>(type: "TEXT", nullable: false),
|
||||
Belopp = table.Column<double>(type: "REAL", nullable: false),
|
||||
Avisering = table.Column<string>(type: "TEXT", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_AccountRecords", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Members",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
FirstName = table.Column<string>(type: "TEXT", nullable: false),
|
||||
LastName = table.Column<string>(type: "TEXT", nullable: false),
|
||||
NickName = table.Column<string>(type: "TEXT", nullable: false),
|
||||
PersonType = table.Column<string>(type: "TEXT", nullable: false),
|
||||
Email = table.Column<string>(type: "TEXT", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Members", x => x.Id);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "AccountRecords");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Members");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using WinFormDiApp.DAL;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace WinFormDiApp.DAL.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "7.0.10");
|
||||
|
||||
modelBuilder.Entity("WinFormDiApp.BL.Models.AccountRecord", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Avisering")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<double>("Belopp")
|
||||
.HasColumnType("REAL");
|
||||
|
||||
b.Property<DateTime>("BetalDatum")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Konto")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Mottagare")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("AccountRecords");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("WinFormDiApp.BL.Models.Member", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("FirstName")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("LastName")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("NickName")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("PersonType")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Members");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
29
WinFormDiApp.DAL/WinFormDiApp.DAL.csproj
Normal file
29
WinFormDiApp.DAL/WinFormDiApp.DAL.csproj
Normal file
@ -0,0 +1,29 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.10">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\WinFormDiApp.BL\WinFormDiApp.BL.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user