0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ASP.NET MVCでアプリを作成してみた【MVC Crud App】

Last updated at Posted at 2023-10-16

ASP.NETとは?

Microsoftが開発・提供しているWebアプリケーションフレームワークのこと
image.png

ASP.NET の3つの重要な特徴

  • コード ビハインド: ASP.Net はコードと設計を切り離し、アプリケーションの保守性を確保し、このプラットフォームのファイルは複数のファイルに分離されているため、開発者は aspx アプリケーションを簡単に維持できる

  • 状態管理機能: ASP.Net には、状態制御および管理機能が組み込まれていて、この機能は、Web アプリケーションやサービスが状態を記憶するのに役立つため、e コマース サイトのショッピング カート機能の設計でよく使用される

  • キャッシュ: ASP.Net はキャッシュ ストレージを実施し、アプリケーションのパフォーマンスを向上させる。これにより、情報の保存、検索、応答が短時間で完了し、アプリケーションのパフォーマンスが向上する

MVCとは?

ユーザーインターフェイスを持つアプリケーションを実装するためのデザインパターン

  • Model データやロジック、関数などを扱う
  • View 画面
  • Controller 入力を受け取り、ModelとViewへの命令に変換

ASP.NET MVCとは?

ASP.NETにMVCのデザインパターンを適用することで、効率的にWebアプリケーションが開発できる

Entity Frameworkとは?

Microsoftが提供する.NET Frameworkのコンポーネント
リストやオブジェクトへの操作SQLなどのデータベース操作に変換

Code Firstとは?

データ構造を定義したクラス(POCO)を元に、Entity Frameworkが必要となるテーブルを作成。データベースの操作が不要になる。

ASP.NET MVC Application の作成

① プロジェクトの作成する

% dotnet tool install  --global dotnet -ef

% dotnet new mvc -n "プロジェクト名"

% dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore --version 5.0.1
//ASP.NET Core Identity and Entity Framework Core パッケージをバージョン 5.0.1 でインストールするためのコマンド

% dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 5.0.1
//SQLite 用の Entity Framework Core プロバイダーをバージョン 5.0.1 でインストールするためのコマンド

% dotnet add package Microsoft.EntityFrameworkCore.Tools --version 5.0.1
//Entity Framework Core コマンドラインツールをバージョン 5.0.1 でインストールするためのコマンド

% dotnet add package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore --version 5.0.1
//ASP.NET Core Diagnostics EntityFrameworkCore パッケージをバージョン 5.0.1 でインストールするためのコマンド

% dotnet run

② appsettings.jsonを編集した後、dotnet build する

appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "ConnectionStrings": {
    "DefaultConnection": "DetaSource=app.db;Cache=Shared"
  },
  "AllowedHosts": "*"
}
dotnet build
dotnet run

③ DataフォルダとApplicationDbContext.csを作成する

④ ModelsにBooks.csを作成する

dotnet new tool-manifest

dotnet new tool-manifest

dotnet ef migrations add "Initial Migrations"

dotnet ef database update 

② ModelsファイルにBooksEntity.csを作成する

Models/BooksEntity.cs
namespace AmazonBook.Models;

public class BooksEntity {
    public int Id {get; set;}
    public string Title {get; set;}

    public string Author {get; set;}
    public string ISBN {get; set;}
}

④ Program.csを下記のコードを加える

Program.cs
namespace AmazonBook.Data;
using AmazonBooks.Models;
using Microsoft.EntityFrameworkCore;

//省略

builder.services.AddDbContext<ApplicationDbContext>(options => 
options.UseSqlServer(builder.Configuration.GetConnencionString("DefaultConnection")));

⑤ データベースとテーブルを作成する

dotnet ef migrations add "InitialDBSetup"
dotnet ef database update

参考サイト

【入門者向け】ASP.NET MVCでWebアプリ開発のノウハウを学ぼう!

.NET アプリケーション開発入門
【2023】asp net とは?人気のWeb開発フレームワーと今後のトレンド
Learn Visual Studio Code Editor for ASP.NET MVC Application - Complete Tutorial
Learn Visual Studio Code Editor for ASP.NET MVC Application - Complete Tutorial
ASP.NET Core MVC CRUD Operations with EF Code First in Visual Studio Code - Part(1)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?