Skip to content

Instantly share code, notes, and snippets.

@prozacgod
Created April 11, 2019 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prozacgod/8d46fa764658250f514973008c31a3d9 to your computer and use it in GitHub Desktop.
Save prozacgod/8d46fa764658250f514973008c31a3d9 to your computer and use it in GitHub Desktop.
My first attempt at some dotnetcore / linux program that creates a database / tables using postgresql and entityframework
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace testApp
{
public class AppContext : DbContext
{
public DbSet<Client> Clients { get; set; }
public DbSet<Patient> Patients { get; set; }
//connectionString="host=hostname;port=5432;database=databaseName;user id=userName;password=secret"
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("host=localhost;database=prozacgod;username=prozacgod;password=saturn");
}
}
public class Client
{
public int ClientId { get; set; }
public string Name { get; set; }
public List<Patient> Patients { get; set; }
}
public class Patient
{
public int PatientId {get; set;}
public string name {get; set;}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var context = new AppContext();
context.Database.EnsureCreated();
var patient1 = new Patient() { name = "Fluffy" };
var patient2 = new Patient() { name = "Fluffy" };
var client = new Client() { Name = "David", Patients = new List<Patient>() { patient1, patient2 } };
context.Add(client);
context.Add(patient1);
context.Add(patient2);
context.SaveChanges();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment