ユーザ用ツール

サイト用ツール


サイドバー

dotnet:webapiチュートリアル

webapiチュートリアル

コンテナの起動

Todoアプリを作成するのは以下のコンテナ。

Dcokerfile
FROM mcr.microsoft.com/dotnet/sdk:6.0

エラー: dotnet-aspnet-codegenerator: command not found

このエラーがでてスキャフォールディングができない。

dotnet tool install -g dotnet-aspnet-codegenerator

の後に

> cat << \EOF >> ~/.bash_profile
# Add .NET Core SDK tools
export PATH="$PATH:/roo/.dotnet/tools"

> export PATH="$PATH:/root/.donet/tools"

を入力する

# dotnet-aspnet-codegenerator controller -name TodoItemsController -async -api -m TodoItem -dc TodoContext -outDir Controllers
Building project ...
Finding the generator 'controller'...
Running the generator 'controller'...

Minimal hosting scenario!
Attempting to compile the application in memory.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'TodoItem'
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 6.0.8 initialized 'TodoContext' using provider 'Microsoft.EntityFrameworkCore.InMemory:6.0.8' with options: StoreName=TodoList 
Added Controller : '/Controllers/TodoItemsController.cs'.
RunTime 00:00:09.35

エラー: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Migrations.IMigrator'.

チュートリアルにあるUseInMemoryDatabaseを使っていると、dotnet ef(entity framework)が利用できない。

# dotnet ef migrations add initial
Build started...
Build succeeded.
 ~
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Migrations.IMigrator'. This is often because no database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

まずはqlserverのコンテナを作成して、そこにつなげる必要がある。
同じdockernetwork内のコンテナどうしなら、接続にコンテナ名が利用できる。
ただしdocker-compsoeを使っていると、「project_db_1」のような名前になっているので注意。
またホストのSSMS(SqlServer Management Stusido) から、コンテナのsqlseverにつなげる場合、すでにホストでSQLSERVERが起動していれば127.0.0.1,14331にする必要がある。
※localhostではつながらない。
※「:」ではなく「,」でポート番号を指定する必要がある。

sqlserverの起動しているコンテナにexecする場合は

> docker exec -it project-dy-1 /opt/mssql-tools/bin/sqlcmd -U sa -P [password]
1 > selecvt @@version
2 > go
 ~
dotnet/webapiチュートリアル.txt · 最終更新: 2022/09/01 14:10 by ips