Last active
July 19, 2020 03:55
-
-
Save kasecato/23f7b0a4a8c47bf67651 to your computer and use it in GitHub Desktop.
macOS ではじめる ASP.NET Core x Entity Framework Core x PostgreSQL ref: https://qiita.com/kasecato/items/134256c4954aa6115502
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace StarWars.Model | |
{ | |
public class Director | |
{ | |
public int DirectorId { get; set; } | |
public int Episode { get; set; } | |
public string Name { get; set; } | |
public DateTime Born { get; set; } // 追加 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
db: | |
build: ./postgres | |
container_name: postgres-starwars | |
environment: | |
POSTGRES_USER: c3po | |
POSTGRES_PASSWORD: r2d2 | |
POSTGRES_DB: starwars | |
ports: | |
- 5432:5432 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM postgres:11 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ brew cask install dotnet-sdk |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StarWars | |
├── project.csproj | |
├── docker-compose.yml | |
├── src | |
│ ├── Domain | |
│ │ └── StarWarsContext.cs | |
│ ├── Model | |
│ │ └── Director.cs | |
│ └── Program.cs | |
└── test | |
└── StarWarsTest.cs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet tool update --global dotnet-ef --version 3.1.6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet ef migrations add StarWarsEp7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet ef database update |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker exec -it postgres-starwars bash | |
# psql -U c3po -d starwars | |
# \d "Directors" | |
Table "public.Directors" | |
Column | Type | Collation | Nullable | Default | |
------------+---------+-----------+----------+------------------------------------------------- | |
DirectorId | integer | | not null | nextval('"Directors_DirectorId_seq"'::regclass) | |
Episode | integer | | not null | | |
Name | text | | | | |
Indexes: | |
"PK_Directors" PRIMARY KEY, btree ("DirectorId") | |
# \quit | |
# exit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet ef migrations add StarWarsEp7.2 | |
$ dotnet ef database update |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet ef migrations add StarWarsEp7.2 | |
$ dotnet ef database update |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker exec -it postgres-starwars bash | |
# psql -U c3po -d starwars | |
# \d "Directors" | |
Table "public.Directors" | |
Column | Type | Collation | Nullable | Default | |
------------+-----------------------------+-----------+----------+---------------------------------------------------- | |
DirectorId | integer | | not null | nextval('"Directors_DirectorId_seq"'::regclass) | |
Episode | integer | | not null | | |
Name | text | | | | |
Born | timestamp without time zone | | not null | '0001-01-01 00:00:00'::timestamp without time zone | |
Indexes: | |
"PK_Directors" PRIMARY KEY, btree ("DirectorId") | |
# \quit | |
# exit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet test | |
Test run for /Users/kasecato/GitHub/kasecato/osxcsharp/bin/Debug/netcoreapp3.1/StarWars.dll(.NETCoreApp,Version=v3.1) | |
Microsoft (R) Test Execution Command Line Tool Version 16.6.0 | |
Copyright (c) Microsoft Corporation. All rights reserved. | |
Starting test execution, please wait... | |
A total of 1 test files matched the specified pattern. | |
Test Run Successful. | |
Total tests: 1 | |
Passed: 1 | |
Total time: 2.2642 Seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet restore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker exec -it postgres-starwars bash | |
# psql -U c3po -d starwars | |
# select * from "Directors"; | |
DirectorId | Episode | Name | Born | |
------------+---------+--------------+--------------------- | |
4 | 7 | J. J. Abrams | 1966-06-27 00:00:00 | |
# \quit | |
# exit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo nuget update -self | |
$ nuget locals all -clear | |
$ sudo rm -rf /usr/local/share/dotnet/ | |
# 最新の .NET Core 1.1 SDK をインストールする |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet migrate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet restore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dnvm upgrade -r mono |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet ef migrations add StarWarsEp7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker-compose up |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker-compose up |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker-compose up |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ dotnet tool install --global dotnet-ef --version 3.1.6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace StarWars | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<AssemblyName>StarWars</AssemblyName> | |
<PackageId>StarWars</PackageId> | |
<VersionPrefix>1.0.0</VersionPrefix> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
<OutputType>Library</OutputType> | |
<DebugType>portable</DebugType> | |
<GenerateDocumentationFile>false</GenerateDocumentationFile> | |
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> | |
<RuntimeFrameworkVersion>3.1.6</RuntimeFrameworkVersion> | |
</PropertyGroup> | |
<ItemGroup> | |
<!-- xUnit.net --> | |
<!-- https://xunit.github.io/docs/getting-started/netcore/cmdline --> | |
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> | |
<PackageReference Include="xunit" Version="2.4.1" /> | |
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" /> | |
<!-- Npgsql Entity Framework Core --> | |
<!-- http://www.npgsql.org/efcore/index.html --> | |
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" /> | |
<!-- The dotnet ef commands are included in the .NET Core SDK, but to enable the commands you have to install the Microsoft.EntityFrameworkCore.Design package. --> | |
<!-- https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#ef-core-3x --> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.6" /> | |
</ItemGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"buildOptions": { | |
"emitEntryPoint": true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore; | |
using StarWars.Model; | |
namespace StarWars.Domain | |
{ | |
public class StarWarsContext : DbContext | |
{ | |
public DbSet<Director> Directors { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
=> optionsBuilder.UseNpgsql("Host=localhost;Database=starwars;Port=15432;Username=c3po;Password=r2d2"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using StarWars.Domain; | |
using StarWars.Model; | |
using Xunit; | |
namespace StarWars.Test | |
{ | |
public class StarWarsCrudTest | |
{ | |
[Fact] | |
public void CreateTest() | |
{ | |
using (var db = new StarWarsContext()) | |
{ | |
// arrange | |
var dirEp7 = new Director() | |
{ | |
DirectorId = 4, | |
Episode = 7, | |
Name = "J. J. Abrams", | |
Born = DateTime.Parse("June 27, 1966") | |
}; | |
// act | |
// SELECT | |
var dirEp7Db = db.Directors.Find(dirEp7.DirectorId); | |
// DELETE | |
if (dirEp7Db != null) | |
{ | |
db.Directors.Remove(dirEp7Db); | |
db.SaveChanges(); | |
} | |
// INSERT | |
db.Directors.Add(dirEp7); | |
var countIns = db.SaveChanges(); | |
// SELECT | |
dirEp7Db = db.Directors.Find(dirEp7.DirectorId); | |
// assert | |
Assert.Equal(1, countIns); | |
Assert.Equal("J. J. Abrams", dirEp7Db.Name); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment