Skip to content

Instantly share code, notes, and snippets.

@2garryn
Created February 18, 2019 10:01
Show Gist options
  • Save 2garryn/4714f6f31e5c8bc3c94e93cf8999e8a2 to your computer and use it in GitHub Desktop.
Save 2garryn/4714f6f31e5c8bc3c94e93cf8999e8a2 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System;
namespace Atrades.Accounts
{
public class AccountsContext: DbContext
{
public DbSet<Account> Accounts { set; get; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>().ToTable("myaccounts");
modelBuilder.Entity<Account>().HasKey(a => a.UserId);
modelBuilder.Entity<Account>().HasIndex(a => a.Username).IsUnique();
modelBuilder.Entity<Account>().HasIndex(a => a.Email).IsUnique();
modelBuilder.Entity<AccountConfirmation>().ToTable("myconfirm");
modelBuilder.Entity<AccountConfirmation>().HasKey(a => a.ConfirmId);
modelBuilder.Entity<Session>().ToTable("mysessions");
modelBuilder.Entity<Session>().HasKey(a => a.SessionId);
modelBuilder.Entity<PwdRecovery>().ToTable("myrecovery");
modelBuilder.Entity<PwdRecovery>().HasKey(a => a.RecoveryId);
modelBuilder.Entity<Session>()
.HasOne(s => s.Account)
.WithMany(u => u.Sessions)
.HasForeignKey(u => u.UserId)
.HasConstraintName("ForeignKey_Session_User");
modelBuilder.Entity<PwdRecovery>()
.HasOne(u => u.Account)
.WithOne(p => p.Recovery)
.HasForeignKey<PwdRecovery>(u => u.UserId)
.HasConstraintName("ForeignKey_PwdRecovery_User");
modelBuilder.Entity<AccountConfirmation>()
.HasOne(u => u.Account)
.WithOne(u => u.Confirmation)
.HasForeignKey<AccountConfirmation>(u => u.UserId)
.HasConstraintName("ForeignKey_AccountConfirmation_User");
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseNpgsql("Host=localhost;Port=32769;Database=atrades;Username=main;Password=main");
}
public class Account
{
private int UserId {get; set;}
[Column(TypeName = "varchar(64)"), Required]
private string Username {get; set;}
[Column(TypeName = "varchar(320)"), Required]
private string Email {get; set;}
private bool Blocked {get; set;}
private bool Confirmed {get; set;}
private DateTime OnCreated {get; set;}
private string PwdHash {get; set;}
private string PwdSalt {get; set;}
private DateTime PwdUpdatedTs {get; set;}
private List<Session> Sessions {get; set;}
private PwdRecovery Recovery {get; set;}
private AccountConfirmation Confirmation {get; set;}
}
public class AccountConfirmation {
private string ConfirmId {get; set;}
private int UserId {get; set;}
private DateTime CreatedOn {get; set;}
private Account Account {get; set;}
}
public class Session {
private string SessionId {get; set;}
private string UserAgent {get; set;}
private int UserId {get; set;}
private DateTime CreatedOn {get; set;}
private Account Account {get; set;}
}
public class PwdRecovery {
private string RecoveryId {get; set;}
private int UserId {get; set;}
private DateTime CreatedOn {get; set;}
private Account Account {get; set;}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment