1 Commits

Author SHA1 Message Date
b2484e18a5 капец 2025-06-07 20:07:52 +04:00
10 changed files with 185 additions and 0 deletions

View File

@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BussinessLogick", "BussinessLogick\BussinessLogick.csproj", "{D098214E-146D-4B68-9EF7-89F5DB610CB7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataBase", "DataBase\DataBase.csproj", "{608AC528-CB79-4E4E-BFF6-7EF154A56F6C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
{D098214E-146D-4B68-9EF7-89F5DB610CB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D098214E-146D-4B68-9EF7-89F5DB610CB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D098214E-146D-4B68-9EF7-89F5DB610CB7}.Release|Any CPU.Build.0 = Release|Any CPU
{608AC528-CB79-4E4E-BFF6-7EF154A56F6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{608AC528-CB79-4E4E-BFF6-7EF154A56F6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{608AC528-CB79-4E4E-BFF6-7EF154A56F6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{608AC528-CB79-4E4E-BFF6-7EF154A56F6C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -6,4 +6,11 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1;
public interface IConfigurationDatabase
{
string ConnectionString { get; }
}

14
ClassLibrary1/Program.cs Normal file
View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1;
public class Program
{
public static void Main(string[] args)
{
}
}

21
DataBase/Article.cs Normal file
View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBase;
internal class Article
{
[Key]
public required string Id { get; set; }
public required string Title { get; set; }
public required string Tema { get; set; }
public DateTime DateCreat { get; set; }
[ForeignKey("ArticleId")]
public List<Author>? Authors { get; set; }
}

23
DataBase/Author.cs Normal file
View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBase;
internal class Author
{
[Key]
public required string Id { get; set; }
public required string FIO { get; set; }
public required string Email { get; set; }
public DateTime BirthDate { get; set; }
public required string Work { get; set; }
public required string ArticleId { get; set; }
[ForeignKey("ArticleId")]
public Article? Article { get; set; }
}

27
DataBase/DataBase.csproj Normal file
View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,13 @@
using ClassLibrary1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBase;
class DefaultConfigurationDatabase : IConfigurationDatabase
{
public string ConnectionString => "";
}

View File

@@ -0,0 +1,45 @@
using ClassLibrary1;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
namespace DataBase;
internal class LibraryDbContext : DbContext
{
private readonly IConfigurationDatabase? _configurationDatabase;
public LibraryDbContext(IConfigurationDatabase configurationDatabase)
{
_configurationDatabase = configurationDatabase;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString, o => o.SetPostgresVersion(12, 2));
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Author>().HasIndex(x => x.Email).IsUnique();
modelBuilder.Entity<Author>()
.HasOne(a => a.Article)
.WithMany(a => a.Authors)
.HasForeignKey(a => a.ArticleId)
.OnDelete(DeleteBehavior.Restrict);
}
public DbSet<Article> Articles { get; set; }
public DbSet<Author> Authors { get; set; }
}

View File

@@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBase;
internal class SampleContextFactory : IDesignTimeDbContextFactory<LibraryDbContext>
{
public LibraryDbContext CreateDbContext(string[] args)
{
return new LibraryDbContext(new DefaultConfigurationDatabase());
}
}