DbContext
This commit is contained in:
parent
60e193a47a
commit
b6b6d136d7
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PuferFishContracts.Infrastructure;
|
||||
|
||||
public interface IConfigurationDatabase
|
||||
{
|
||||
string ConnectionString { get; }
|
||||
}
|
@ -6,6 +6,11 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PuferFishContracts\PuferFishContracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
51
PuferFishContracts/PuferFishDataBase/PuferFishDbContext.cs
Normal file
51
PuferFishContracts/PuferFishDataBase/PuferFishDbContext.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PuferFishContracts.Infrastructure;
|
||||
using PuferFishDataBase.Models;
|
||||
|
||||
namespace PuferFishDataBase;
|
||||
|
||||
internal class PuferFishDbContext(IConfigurationDatabase configurationDatabase) : DbContext
|
||||
{
|
||||
private readonly IConfigurationDatabase? _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<Buyer>().HasIndex(x => x.PhoneNumber).IsUnique();
|
||||
modelBuilder.Entity<Post>()
|
||||
.HasIndex(e => new { e.PostName, e.IsActual })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||
modelBuilder.Entity<Post>()
|
||||
.HasIndex(e => new { e.PostId, e.IsActual })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||
modelBuilder.Entity<Product>()
|
||||
.HasIndex(x => new { x.ProductName, x.IsDeleted })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Product.IsDeleted)}\" = FALSE");
|
||||
modelBuilder.Entity<SaleProduct>().HasKey(x => new {
|
||||
x.SaleId,
|
||||
x.ProductId
|
||||
});
|
||||
}
|
||||
|
||||
public DbSet<Buyer> Buyers { get; set; }
|
||||
public DbSet<Post> Posts { get; set; }
|
||||
public DbSet<Product> Products { get; set; }
|
||||
public DbSet<ProductHistory> ProductHistories { get; set; }
|
||||
public DbSet<Points> Pointss { get; set; }
|
||||
public DbSet<Sale> Sales { get; set; }
|
||||
public DbSet<SaleProduct> SaleProducts { get; set; }
|
||||
public DbSet<Worker> Workers { get; set; }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user