Вытягивание существующей бд

This commit is contained in:
Павел Путилин 2023-04-19 14:32:05 +04:00
parent 13e33f4ce8
commit d59f55d549
9 changed files with 281 additions and 0 deletions

25
News/News.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32901.215
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "News", "News\News.csproj", "{985E9E32-067A-4DB6-8AE5-19A7495E3CC4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{985E9E32-067A-4DB6-8AE5-19A7495E3CC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{985E9E32-067A-4DB6-8AE5-19A7495E3CC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{985E9E32-067A-4DB6-8AE5-19A7495E3CC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{985E9E32-067A-4DB6-8AE5-19A7495E3CC4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3950A75-07C0-4805-9081-FD72CA18F14C}
EndGlobalSection
EndGlobal

16
News/News/Author.cs Normal file
View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
namespace News;
/// <summary>
/// авторы
/// </summary>
public partial class Author
{
public int AuthorId { get; set; }
public string AuthorName { get; set; } = null!;
public virtual ICollection<News> News { get; set; } = new List<News>();
}

22
News/News/Category.cs Normal file
View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
namespace News;
/// <summary>
/// категории
/// </summary>
public partial class Category
{
/// <summary>
/// ид категории
/// </summary>
public int CategoryId { get; set; }
/// <summary>
/// наименование
/// </summary>
public string CategoryName { get; set; } = null!;
public virtual ICollection<News> News { get; set; } = new List<News>();
}

20
News/News/Comment.cs Normal file
View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
namespace News;
/// <summary>
/// комментарии
/// </summary>
public partial class Comment
{
public int UserId { get; set; }
public int NewsId { get; set; }
public string CommentText { get; set; } = null!;
public virtual News News { get; set; } = null!;
public virtual User User { get; set; } = null!;
}

26
News/News/News.cs Normal file
View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
namespace News;
/// <summary>
/// новости
/// </summary>
public partial class News
{
public int NewsId { get; set; }
public string NewsName { get; set; } = null!;
public DateOnly? NewsDate { get; set; }
public int AuthorId { get; set; }
public int CategoryId { get; set; }
public virtual Author Author { get; set; } = null!;
public virtual Category Category { get; set; } = null!;
public virtual ICollection<Comment> Comments { get; set; } = new List<Comment>();
}

19
News/News/News.csproj Normal file
View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ConsoleTableExt" Version="3.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
</Project>

135
News/News/NewsContext.cs Normal file
View File

@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace News;
public partial class NewsContext : DbContext
{
public NewsContext()
{
}
public NewsContext(DbContextOptions<NewsContext> options)
: base(options)
{
}
public virtual DbSet<Author> Authors { get; set; }
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Comment> Comments { get; set; }
public virtual DbSet<News> News { get; set; }
public virtual DbSet<User> Users { 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=192.168.56.103;Port=5432;Database=news;Username=postgres;Password=54050");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Author>(entity =>
{
entity.HasKey(e => e.AuthorId).HasName("author_pkey");
entity.ToTable("author", tb => tb.HasComment("авторы"));
entity.Property(e => e.AuthorId)
.HasDefaultValueSql("nextval('author_id_seq'::regclass)")
.HasColumnName("author_id");
entity.Property(e => e.AuthorName)
.HasMaxLength(150)
.HasColumnName("author_name");
});
modelBuilder.Entity<Category>(entity =>
{
entity.HasKey(e => e.CategoryId).HasName("category_pkey");
entity.ToTable("category", tb => tb.HasComment("категории"));
entity.Property(e => e.CategoryId)
.HasDefaultValueSql("nextval('category_id_seq'::regclass)")
.HasComment("ид категории")
.HasColumnName("category_id");
entity.Property(e => e.CategoryName)
.HasMaxLength(150)
.HasComment("наименование")
.HasColumnName("category_name");
});
modelBuilder.Entity<Comment>(entity =>
{
entity.HasKey(e => new { e.UserId, e.NewsId }).HasName("comment_pkey");
entity.ToTable("comment", tb => tb.HasComment("комментарии"));
entity.Property(e => e.UserId).HasColumnName("user_id");
entity.Property(e => e.NewsId).HasColumnName("news_id");
entity.Property(e => e.CommentText)
.HasMaxLength(2000)
.HasColumnName("comment_text");
entity.HasOne(d => d.News).WithMany(p => p.Comments)
.HasForeignKey(d => d.NewsId)
.HasConstraintName("news_id");
entity.HasOne(d => d.User).WithMany(p => p.Comments)
.HasForeignKey(d => d.UserId)
.HasConstraintName("user_id");
});
modelBuilder.Entity<News>(entity =>
{
entity.HasKey(e => e.NewsId).HasName("news_pkey");
entity.ToTable("news", tb => tb.HasComment("новости"));
entity.Property(e => e.NewsId)
.HasDefaultValueSql("nextval('news_id_seq'::regclass)")
.HasColumnName("news_id");
entity.Property(e => e.AuthorId).HasColumnName("author_id");
entity.Property(e => e.CategoryId).HasColumnName("category_id");
entity.Property(e => e.NewsDate).HasColumnName("news_date");
entity.Property(e => e.NewsName)
.HasMaxLength(200)
.HasColumnName("news_name");
entity.HasOne(d => d.Author).WithMany(p => p.News)
.HasForeignKey(d => d.AuthorId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("author_id");
entity.HasOne(d => d.Category).WithMany(p => p.News)
.HasForeignKey(d => d.CategoryId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("category_id");
});
modelBuilder.Entity<User>(entity =>
{
entity.HasKey(e => e.UserId).HasName("user_pkey");
entity.ToTable("user", tb => tb.HasComment("пользователи"));
entity.Property(e => e.UserId)
.HasDefaultValueSql("nextval('user_id_seq'::regclass)")
.HasColumnName("user_id");
entity.Property(e => e.UserName)
.HasMaxLength(150)
.HasColumnName("user_name");
});
modelBuilder.HasSequence("author_id_seq");
modelBuilder.HasSequence("category_id_seq");
modelBuilder.HasSequence("comment_id_seq");
modelBuilder.HasSequence("news_id_seq");
modelBuilder.HasSequence("user_id_seq");
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

2
News/News/Program.cs Normal file
View File

@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

16
News/News/User.cs Normal file
View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
namespace News;
/// <summary>
/// пользователи
/// </summary>
public partial class User
{
public int UserId { get; set; }
public string UserName { get; set; } = null!;
public virtual ICollection<Comment> Comments { get; set; } = new List<Comment>();
}