Skip to content

Instantly share code, notes, and snippets.

@farukcan
Last active May 2, 2023 15:34
Show Gist options
  • Save farukcan/93b2dca00f18792932ac1263e352fad5 to your computer and use it in GitHub Desktop.
Save farukcan/93b2dca00f18792932ac1263e352fad5 to your computer and use it in GitHub Desktop.
Postgress net core

Program.cs

Note: i've more a gist about it.

using Microsoft.EntityFrameworkCore;
builder.Services.AddDbContext<Context>(options =>
    options.UseNpgsql(System.Environment.GetEnvironmentVariable("POSTGRESQL_STRING") ?? throw new InvalidOperationException("Connection string 'POSTGRESQL_STRING' not found."),
    options => options.EnableRetryOnFailure().SetPostgresVersion(new Version(9, 6))
));
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

Context.cs

using Microsoft.EntityFrameworkCore;
using NotionQuickBlog.Helpers;
using NotionQuickBlog.Model;
using NotionQuickBlog.Models;

namespace NotionQuickBlog.Data
{
    public class Context : DbContext
    {
        public Context(DbContextOptions options) : base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            // Default Customers
            List<Customer> customers = new();
            customers.Add(new Customer()
            {
                Email = "omer@farukcan.net",
                PasswordHash = "12345678".GetSha256Hash("omer@farukcan.net"),
                IsAdmin = true
            });
            modelBuilder.Entity<Blog>().Property(x => x.Slug).HasColumnName("Slug");
            modelBuilder.Entity<Customer>().HasData(customers);
            List<SiteBuilder> builders = new();
            builders.Add(new SiteBuilder()
            {
                Name = "Daisy",
                Repository = "NotionQuickBlog/daisy-nqb",
                Sample = "https://sample.nquick.blog",
                Image = "https://user-images.githubusercontent.com/7342023/162526541-0aa9ac3b-496e-4604-a6fb-9c2e76451a79.png"
            });
            modelBuilder.Entity<Ticket>()
                .HasOne(t => t.Blog)
                .WithOne(b => b.Ticket)
                .HasForeignKey<Blog>(b => b.TicketId);
            modelBuilder.Entity<SiteBuilder>().HasData(builders);

            modelBuilder.Entity<Ticket>().Navigation(e => e.Blog).AutoInclude();
            modelBuilder.Entity<Ticket>().Navigation(e => e.Owner).AutoInclude();
            modelBuilder.Entity<Ticket>().Navigation(e => e.SiteTemplate).AutoInclude();
            modelBuilder.Entity<Blog>().Navigation(e => e.Ticket).AutoInclude();
            modelBuilder.Entity<Blog>().Navigation(e => e.Customer).AutoInclude();
            modelBuilder.Entity<Blog>().Navigation(e => e.Builder).AutoInclude();
            modelBuilder.Entity<SiteTemplate>().Navigation(e => e.Owner).AutoInclude();
        }
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<SiteBuilder> Builders { get; set; }
        public DbSet<Ticket> Tickets { get; set; }
        public DbSet<SiteTemplate> Templates { get; set; }
    }
}

Model sample

using System.ComponentModel.DataAnnotations;

namespace your.Model
{
    public class SiteBuilder
    {
        // --- Structure
        [Key, MinLength(2), MaxLength(256)]
        public string? Name { get; set; } // primary
        [MaxLength(512)]
        public string? Repository { get; set; }
        [MaxLength(512)]
        public string? Sample { get; set; }
        [MaxLength(512)]
        public string? Image { get; set; }
        public string Version { get; set; } = "0.0.1";
        public string? RepositoryName => Repository?.Split("/")[^1];
        public string? RepositoryOwner => Repository?.Split("/")[^2];
        public DateTime CreatedAt { get; set; } = DateTime.Now;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment