Рабочая интеграция с Docker БД

This commit is contained in:
maksim 2024-11-11 17:21:51 +04:00
parent e768af07da
commit 1f48b7d471
9 changed files with 213 additions and 1 deletions

8
App.config Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="EmployeeDB"
connectionString="Host=localhost;Port=5432;Database=EmployeeDB;Username=postgres;Password=yourpassword;"
providerName="Npgsql" />
</connectionStrings>
</configuration>

View File

@ -3,6 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:EmployeeManagementApp">
<Application.Resources>
</Application.Resources>
</Application>

View File

@ -8,4 +8,15 @@
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MySql.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.10" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,55 @@
// <auto-generated />
using EmployeeManagementApp.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace EmployeeManagementApp.Migrations
{
[DbContext(typeof(EmployeeContext))]
[Migration("20241111130759_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("EmployeeManagementApp.Models.Employee", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Position")
.IsRequired()
.HasColumnType("longtext");
b.Property<decimal>("Salary")
.HasColumnType("decimal(18,2)");
b.Property<int>("VacationDays")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Employees");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore.Migrations;
using MySql.EntityFrameworkCore.Metadata;
#nullable disable
namespace EmployeeManagementApp.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySQL:Charset", "utf8mb4");
migrationBuilder.CreateTable(
name: "Employees",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySQL:ValueGenerationStrategy", MySQLValueGenerationStrategy.IdentityColumn),
FirstName = table.Column<string>(type: "longtext", nullable: false),
LastName = table.Column<string>(type: "longtext", nullable: false),
Position = table.Column<string>(type: "longtext", nullable: false),
Salary = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
VacationDays = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Employees", x => x.Id);
})
.Annotation("MySQL:Charset", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Employees");
}
}
}

View File

@ -0,0 +1,52 @@
// <auto-generated />
using EmployeeManagementApp.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace EmployeeManagementApp.Migrations
{
[DbContext(typeof(EmployeeContext))]
partial class EmployeeContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("EmployeeManagementApp.Models.Employee", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("Position")
.IsRequired()
.HasColumnType("longtext");
b.Property<decimal>("Salary")
.HasColumnType("decimal(18,2)");
b.Property<int>("VacationDays")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Employees");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -8,6 +10,8 @@ namespace EmployeeManagementApp.Models
{
public class Employee
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }

View File

@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using EmployeeManagementApp.Models;
namespace EmployeeManagementApp.Services
{
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Используем MySQL вместо PostgreSQL
optionsBuilder.UseMySQL("Server=localhost;Port=3306;Database=EmployeeDB;User=root;Password=SuperSecretPasswork2003;");
}
}
}

23
docker-compose.yml Normal file
View File

@ -0,0 +1,23 @@
version: '3.9'
services:
mysql:
image: mysql:8.0
container_name: employee_db
restart: always
environment:
MYSQL_ROOT_PASSWORD: SuperSecretPasswork2003
MYSQL_DATABASE: EmployeeDB
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
networks:
- employee_network
volumes:
mysql_data:
networks:
employee_network:
driver: bridge