From 84dd2cd4edf708dc303edce59cecf3f3221a196e Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Fri, 16 Oct 2020 21:13:12 -0400 Subject: [PATCH] index route works --- Controllers/PeopleController.cs | 28 ++++++++++--- .../20201017005725_InitialCreate.Designer.cs | 39 ++++++++++++++++++ Migrations/20201017005725_InitialCreate.cs | 30 ++++++++++++++ Migrations/PeopleContextModelSnapshot.cs | 37 +++++++++++++++++ Model.cs | 20 +++++++++ Person.cs | 18 -------- contacts.csproj | 5 +++ people.db | Bin 0 -> 20480 bytes 8 files changed, 154 insertions(+), 23 deletions(-) create mode 100644 Migrations/20201017005725_InitialCreate.Designer.cs create mode 100644 Migrations/20201017005725_InitialCreate.cs create mode 100644 Migrations/PeopleContextModelSnapshot.cs create mode 100644 Model.cs delete mode 100644 Person.cs create mode 100644 people.db diff --git a/Controllers/PeopleController.cs b/Controllers/PeopleController.cs index 919c3e7..1ca1f91 100644 --- a/Controllers/PeopleController.cs +++ b/Controllers/PeopleController.cs @@ -12,13 +12,31 @@ namespace contacts.Controllers public class PeopleController : ControllerBase { [HttpGet] + // public Person[] Get() public Person[] Get() { - Person p1 = new Person(1, "Matt", 40); - Person p2 = new Person(2, "Sally", 32); - Person p3 = new Person(3, "Zagthrop", 834); - // Console.WriteLine(p1.name); - return new Person[] { p1, p2, p3 }; + using (var db = new PeopleContext()) + { + // Console.WriteLine("Hello World!"); + // db.Add(new Person { Name = "Matt", Age = 40 }); + // db.SaveChanges(); + + // var person = db.People + var people = db.People + .OrderBy(person => person.PersonId) + // .Last(); + .ToArray(); + // Console.WriteLine(person.Age); + Console.WriteLine(db.People.OrderBy(person => person.PersonId).ToArray()); + + // return person; + return people; + } + // Person p1 = new Person(1, "Matt", 40); + // Person p2 = new Person(2, "Sally", 32); + // Person p3 = new Person(3, "Zagthrop", 834); + // // Console.WriteLine(p1.name); + // return new Person[] { p1, p2, p3 }; } } } diff --git a/Migrations/20201017005725_InitialCreate.Designer.cs b/Migrations/20201017005725_InitialCreate.Designer.cs new file mode 100644 index 0000000..b22ba80 --- /dev/null +++ b/Migrations/20201017005725_InitialCreate.Designer.cs @@ -0,0 +1,39 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using contacts; + +namespace contacts.Migrations +{ + [DbContext(typeof(PeopleContext))] + [Migration("20201017005725_InitialCreate")] + partial class InitialCreate + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "3.1.9"); + + modelBuilder.Entity("contacts.Person", b => + { + b.Property("PersonId") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Age") + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.HasKey("PersonId"); + + b.ToTable("People"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20201017005725_InitialCreate.cs b/Migrations/20201017005725_InitialCreate.cs new file mode 100644 index 0000000..baf512b --- /dev/null +++ b/Migrations/20201017005725_InitialCreate.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace contacts.Migrations +{ + public partial class InitialCreate : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "People", + columns: table => new + { + PersonId = table.Column(nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(nullable: true), + Age = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_People", x => x.PersonId); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "People"); + } + } +} diff --git a/Migrations/PeopleContextModelSnapshot.cs b/Migrations/PeopleContextModelSnapshot.cs new file mode 100644 index 0000000..d82bba3 --- /dev/null +++ b/Migrations/PeopleContextModelSnapshot.cs @@ -0,0 +1,37 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using contacts; + +namespace contacts.Migrations +{ + [DbContext(typeof(PeopleContext))] + partial class PeopleContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "3.1.9"); + + modelBuilder.Entity("contacts.Person", b => + { + b.Property("PersonId") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Age") + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.HasKey("PersonId"); + + b.ToTable("People"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Model.cs b/Model.cs new file mode 100644 index 0000000..2423420 --- /dev/null +++ b/Model.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; + +namespace contacts +{ + public class PeopleContext : DbContext + { + public DbSet People { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlite("Data Source=people.db"); + } + + public class Person + { + public int PersonId { get; set; } + public string Name { get; set; } + public int Age { get; set; } + } +} diff --git a/Person.cs b/Person.cs deleted file mode 100644 index 65fb02f..0000000 --- a/Person.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -namespace contacts -{ - public class Person - { - public int id { get; set; } - public string name { get; set; } - public int age { get; set; } - public Person(int idParam, string nameParam, int ageParam) - { - - this.id = idParam; - this.name = nameParam; - this.age = ageParam; - } - } -} diff --git a/contacts.csproj b/contacts.csproj index 13f684e..5e56310 100644 --- a/contacts.csproj +++ b/contacts.csproj @@ -11,6 +11,11 @@ + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + diff --git a/people.db b/people.db new file mode 100644 index 0000000000000000000000000000000000000000..8032d86a5f4388fce42ff430a6966eb62a009478 GIT binary patch literal 20480 zcmeI)&uiLX7zgn8P10Y2&0q}iF!mxlC@rFPBD*b>cU!1NXXBWM5z?BKfKAr;3fr;$ zfA*K`yvt6z?^?!=do)^FvAyghd>@jSJiK}GKA+^`4?KR?3RNIJ#mTv^!~uK6IA^bf zV2l~GZqT|2jdrHFMexPW1Fr@vzYtmTFEg^=+3pYX+wN1^K|lZk5P$##AOHafKmY;| zfWZFKHO;bq~%KCT8Awumc|w%|2){IGfS6s zeJhvG^D8;4*$v{)qafWHcPbRpWI+@&f`Sf{aU9vhg0LM|zL7oQbX?)|TP@M-ILB_! zqC5>d@)jp{dv@FE4aAWg2&?aQY=wpMX_vnjS_$rK}@!N2$;^gbfq;|Kfm1Az2yeT>DGo9HzP2N-IR?qW(x=B2o zoT~T4M^jy@nkm`ZX6|EyvoIP4U*>nspQw0t?5(_tS6!Lb=|S6`K5SW9x%rdPf`9-7 zAOHafKmY;|fB*y_009U?l;BKWZj{uY!Yewfr(e zeR1;_qXhv02tWV=5P$##AOHafKmY;|fWVpxJk#aQLmvW6zyH_F-^~0&8w3O(009U< z00Izz00bZa0SG_<0_!fYqZ#Z8Z~ICWx4FR{^TYT=OMm}=&CKg{Zx8c_00bZa0SG_< Z0uX=z1Rwwb2teSrK!)pip8oNI_7C@j+WP