この文書の現在のバージョンと選択したバージョンの差分を表示します。
次のリビジョン | 前のリビジョン | ||
dotnet:webapiチュートリアル [2022/09/01 13:36] ips 作成 |
dotnet:webapiチュートリアル [2022/09/01 14:10] (現在) ips |
||
---|---|---|---|
ライン 2: | ライン 2: | ||
[[https://docs.microsoft.com/ja-jp/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio-code|チュートリアル: ASP.NET Core で Web API を作成する]] | [[https://docs.microsoft.com/ja-jp/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio-code|チュートリアル: ASP.NET Core で Web API を作成する]] | ||
+ | |||
+ | ===== コンテナの起動 ===== | ||
+ | |||
Todoアプリを作成するのは以下のコンテナ。 | Todoアプリを作成するのは以下のコンテナ。 | ||
ライン 7: | ライン 10: | ||
FROM mcr.microsoft.com/dotnet/sdk:6.0 | FROM mcr.microsoft.com/dotnet/sdk:6.0 | ||
</code> | </code> | ||
+ | |||
+ | ===== エラー: dotnet-aspnet-codegenerator: command not found ===== | ||
+ | |||
+ | このエラーがでてスキャフォールディングができない。 | ||
+ | <code> | ||
+ | dotnet tool install -g dotnet-aspnet-codegenerator | ||
+ | </code> | ||
+ | |||
+ | の後に | ||
+ | |||
+ | <code> | ||
+ | > cat << \EOF >> ~/.bash_profile | ||
+ | # Add .NET Core SDK tools | ||
+ | export PATH="$PATH:/roo/.dotnet/tools" | ||
+ | </code> | ||
+ | |||
+ | と | ||
+ | |||
+ | <code> | ||
+ | > export PATH="$PATH:/root/.donet/tools" | ||
+ | </code> | ||
+ | を入力する | ||
+ | |||
+ | <code> | ||
+ | # 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 | ||
+ | </code> | ||
+ | |||
+ | ===== エラー: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Migrations.IMigrator'. ===== | ||
+ | |||
+ | チュートリアルにあるUseInMemoryDatabaseを使っていると、dotnet ef(entity framework)が利用できない。 | ||
+ | |||
+ | <code> | ||
+ | # 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. | ||
+ | </code> | ||
+ | |||
+ | まずはqlserverのコンテナを作成して、そこにつなげる必要がある。 | ||
+ | 同じdockernetwork内のコンテナどうしなら、接続にコンテナ名が利用できる。 | ||
+ | ただしdocker-compsoeを使っていると、「project_db_1」のような名前になっているので注意。 | ||
+ | またホストのSSMS(SqlServer Management Stusido) から、コンテナのsqlseverにつなげる場合、すでにホストでSQLSERVERが起動していれば<html>127.0.0.1,14331</html>にする必要がある。 | ||
+ | ※localhostではつながらない。 | ||
+ | ※「:」ではなく「,」でポート番号を指定する必要がある。 | ||
+ | |||
+ | sqlserverの起動しているコンテナにexecする場合は | ||
+ | <code> | ||
+ | > docker exec -it project-dy-1 /opt/mssql-tools/bin/sqlcmd -U sa -P [password] | ||
+ | 1 > selecvt @@version | ||
+ | 2 > go | ||
+ | ~ | ||
+ | </code> | ||
+ |