Генерация кода на основе существующей базы данных

This commit is contained in:
m1aksim1 2023-03-13 20:53:28 +04:00
parent f89ab2c654
commit a4e186c78f
13 changed files with 389 additions and 4 deletions

View File

@ -0,0 +1,5 @@
Host=169.254.89.101;
Port=5432;
Database=course;
Username=postgres;
Password=root;

View File

@ -0,0 +1,209 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using WorkTime.Models;
using WorkTime.Models.Views;
namespace WorkTime;
public partial class CourseContext : DbContext
{
public CourseContext()
{
}
public CourseContext(DbContextOptions<CourseContext> options)
: base(options)
{
}
public virtual DbSet<Allowance> Allowances { get; set; }
public virtual DbSet<AllowanceWorker> AllowanceWorkers { get; set; }
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<GetSalaryOnCurrentMonth> GetSalaryOnCurrentMonths { get; set; }
public virtual DbSet<Post> Posts { get; set; }
public virtual DbSet<Project> Projects { get; set; }
public virtual DbSet<SalaryWithAllowance> SalaryWithAllowances { get; set; }
public virtual DbSet<Worker> Workers { get; set; }
public virtual DbSet<WorkingDay> WorkingDays { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseNpgsql("Host=169.254.89.101;Port=5432;Database=course;Username=postgres;Password=root");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Allowance>(entity =>
{
entity.HasKey(e => e.Id).HasName("allowance_pkey");
entity.ToTable("allowance");
entity.Property(e => e.Id)
.ValueGeneratedNever()
.HasColumnName("id");
entity.Property(e => e.Name)
.HasMaxLength(64)
.HasColumnName("name");
entity.Property(e => e.Percent)
.HasPrecision(6, 5)
.HasColumnName("percent");
});
modelBuilder.Entity<AllowanceWorker>(entity =>
{
entity.HasKey(e => new { e.AllowanceId, e.WorkerId }).HasName("allowance_worker_pkey");
entity.ToTable("allowance_worker");
entity.Property(e => e.AllowanceId).HasColumnName("allowance_id");
entity.Property(e => e.WorkerId).HasColumnName("worker_id");
});
modelBuilder.Entity<Customer>(entity =>
{
entity.HasKey(e => e.Id).HasName("customer_pkey");
entity.ToTable("customer");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.Name)
.HasMaxLength(64)
.HasColumnName("name");
});
modelBuilder.Entity<GetSalaryOnCurrentMonth>(entity =>
{
entity
.HasNoKey()
.ToView("get_salary_on_current_month");
entity.Property(e => e.Hours).HasColumnName("hours");
entity.Property(e => e.Name)
.HasColumnType("character varying")
.HasColumnName("name");
entity.Property(e => e.Salary).HasColumnName("salary");
entity.Property(e => e.WorkerId).HasColumnName("worker_id");
});
modelBuilder.Entity<Post>(entity =>
{
entity.HasKey(e => e.Id).HasName("post_pkey");
entity.ToTable("post");
entity.Property(e => e.Id)
.ValueGeneratedNever()
.HasColumnName("id");
entity.Property(e => e.Name)
.HasMaxLength(64)
.HasColumnName("name");
entity.Property(e => e.Salary)
.HasPrecision(10, 2)
.HasColumnName("salary");
});
modelBuilder.Entity<Project>(entity =>
{
entity.HasKey(e => e.Id).HasName("project_pkey");
entity.ToTable("project");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.Description).HasColumnName("description");
entity.Property(e => e.IdCustomer).HasColumnName("id_customer");
entity.Property(e => e.IdDeveloper).HasColumnName("id_developer");
entity.Property(e => e.IdManager).HasColumnName("id_manager");
entity.Property(e => e.Name)
.HasColumnType("character varying")
.HasColumnName("name");
entity.Property(e => e.Ready).HasColumnName("ready");
entity.HasOne(d => d.IdCustomerNavigation).WithMany(p => p.Projects)
.HasForeignKey(d => d.IdCustomer)
.HasConstraintName("project_id_customer_fkey");
entity.HasOne(d => d.IdDeveloperNavigation).WithMany(p => p.ProjectIdDeveloperNavigations)
.HasForeignKey(d => d.IdDeveloper)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("project_id_developer_fkey");
entity.HasOne(d => d.IdManagerNavigation).WithMany(p => p.ProjectIdManagerNavigations)
.HasForeignKey(d => d.IdManager)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("project_id_manager_fkey");
});
modelBuilder.Entity<SalaryWithAllowance>(entity =>
{
entity
.HasNoKey()
.ToView("salary_with_allowance");
entity.Property(e => e.CalcSalary).HasColumnName("calc_salary");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.Name)
.HasMaxLength(64)
.HasColumnName("name");
entity.Property(e => e.Должность)
.HasMaxLength(64)
.HasColumnName("должность");
});
modelBuilder.Entity<Worker>(entity =>
{
entity.HasKey(e => e.Id).HasName("worker_pkey");
entity.ToTable("worker");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.DateEmployment)
.HasColumnType("timestamp without time zone")
.HasColumnName("date_employment");
entity.Property(e => e.Name)
.HasMaxLength(64)
.HasColumnName("name");
entity.Property(e => e.PostId).HasColumnName("post_id");
entity.HasOne(d => d.Post).WithMany(p => p.Workers)
.HasForeignKey(d => d.PostId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("worker_id_post_fkey");
});
modelBuilder.Entity<WorkingDay>(entity =>
{
entity.HasKey(e => e.Id).HasName("working_day_pkey");
entity.ToTable("working_day");
entity.Property(e => e.Id)
.ValueGeneratedNever()
.HasColumnName("id");
entity.Property(e => e.DateEnd)
.HasColumnType("timestamp without time zone")
.HasColumnName("date_end");
entity.Property(e => e.DateStart)
.HasColumnType("timestamp without time zone")
.HasColumnName("date_start");
entity.Property(e => e.WorkerId).HasColumnName("worker_id");
entity.HasOne(d => d.Worker).WithMany(p => p.WorkingDays)
.HasForeignKey(d => d.WorkerId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("working_day_worker_id_fkey");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
namespace WorkTime.Models;
public partial class Allowance
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public decimal Percent { get; set; }
}

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
namespace WorkTime.Models;
public partial class AllowanceWorker
{
public int AllowanceId { get; set; }
public int WorkerId { get; set; }
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
namespace WorkTime.Models;
public partial class Customer
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public virtual ICollection<Project> Projects { get; } = new List<Project>();
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
namespace WorkTime.Models;
public partial class Post
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public decimal Salary { get; set; }
public virtual ICollection<Worker> Workers { get; } = new List<Worker>();
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
namespace WorkTime.Models;
public partial class Project
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public int? IdCustomer { get; set; }
public int IdManager { get; set; }
public bool Ready { get; set; }
public int IdDeveloper { get; set; }
public string Description { get; set; } = null!;
public virtual Customer? IdCustomerNavigation { get; set; }
public virtual Worker IdDeveloperNavigation { get; set; } = null!;
public virtual Worker IdManagerNavigation { get; set; } = null!;
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
namespace WorkTime.Models.Views;
public partial class GetSalaryOnCurrentMonth
{
public int? WorkerId { get; set; }
public string? Name { get; set; }
public decimal? Hours { get; set; }
public decimal? Salary { get; set; }
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
namespace WorkTime.Models.Views;
public partial class SalaryWithAllowance
{
public int? Id { get; set; }
public string? Name { get; set; }
public string? Должность { get; set; }
public decimal? CalcSalary { get; set; }
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
namespace WorkTime.Models;
public partial class Worker
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public int PostId { get; set; }
public DateTime DateEmployment { get; set; }
public virtual Post Post { get; set; } = null!;
public virtual ICollection<Project> ProjectIdDeveloperNavigations { get; } = new List<Project>();
public virtual ICollection<Project> ProjectIdManagerNavigations { get; } = new List<Project>();
public virtual ICollection<WorkingDay> WorkingDays { get; } = new List<WorkingDay>();
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
namespace WorkTime.Models;
public partial class WorkingDay
{
public int Id { get; set; }
public int WorkerId { get; set; }
public DateTime DateStart { get; set; }
public DateTime DateEnd { get; set; }
public virtual Worker Worker { get; set; } = null!;
}

View File

@ -1,2 +1,16 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
namespace WorkTime
{
class Program
{
public static void Main(string[] args)
{
using (var context = new CourseContext())
{
foreach (var salary in context.SalaryWithAllowances)
{
Console.WriteLine($"{salary.Name} {salary.CalcSalary}");
}
}
}
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@ -7,4 +7,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
</Project>