1
3

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 5 years have passed since last update.

ASP.NET MVC 5実践プログラミング

Last updated at Posted at 2019-10-13

はじめに

この記事ではASP.NET MVC 5実践プログラミングをベースに、.NET Core 3.0を学習する方法を書いてみます。
https://www.amazon.co.jp/ASP-NET-MVC-5-%E5%AE%9F%E8%B7%B5%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0-%E5%B1%B1%E7%94%B0%E7%A5%A5%E5%AF%9B-ebook/dp/B00XBF8E9K/

実行環境

下記バージョンで動作確認しています。

  • Windows 10
  • .NET Core 3.0

$ dotnet --version
3.0.100

学習方針

コマンドプロンプトから実行する事で、Mac、Linuxにおいてもそのままできると思います。


$ mkdir MvcBasic
$ cd MvcBasic
$ dotnet new mvc

必要なツールをインストールします。


$ dotnet tool install --global dotnet-ef
$ dotnet tool install --global dotnet-aspnet-codegenerator
$ dotnet tool list --global

必要なパッケージをインストールします。


$ dotnet add package Microsoft.EntityFrameworkCore.Sqlite
$ dotnet add package Microsoft.EntityFrameworkCore.Design
$ dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
$ dotnet add package Microsoft.EntityFrameworkCore.SqlServer

サンプルにならってモデルを作成します。
http://www.wings.msn.to/index.php/-/A-03/978-4-7980-4179-7/

Models/Member.cs
using System;
using System.ComponentModel;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace MvcBasic.Models
{
  public class MvcBasicContext : DbContext
  {
//        public MvcBasicContext (DbContextOptions options) : base(options) {}
        public DbSet<Member> Members { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseSqlite("Data Source=members.db");
  }
  public class Member
  {
    public int Id { get; set; }

    [DisplayName("氏名")]
    public string Name { get; set; }

    [DisplayName("メールアドレス")]
    public string Email { get; set; }

    [DisplayName("生年月日")]
    public DateTime Birth { get; set; }

    [DisplayName("既婚")]
    public bool Married { get; set; }

    [DisplayName("自己紹介")]
    public string Memo { get; set; }
  }
}

モデルからデータベースを生成します。今回データベースにはSQLiteを使います。
https://docs.microsoft.com/ja-jp/ef/core/get-started/?tabs=netcore-cli


$ dotnet ef migrations add InitialCreate
$ dotnet ef database update

ここで実行してみます。
https://localhost:5001/


$ dotnet run
^C

テスト用なのでポート番号は5000のみで良いと思います。

Properties/launchSetting.json
{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:38239",
      "sslPort": 44320
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MvcBasic": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

スキャフォールドでデータベースを確認してみます。
https://mslgt.hatenablog.com/entry/2019/02/15/220944


>dotnet aspnet-codegenerator controller -name MembersController -m Member -dc MvcBasicContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries

ソースコードを一部修正して実行します。これがWindowsでもMacでもLinuxでも動くなんて簡単すぎて驚きます。
http://localhost:5000/Members/

Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MvcBasic.Models;
using Microsoft.EntityFrameworkCore;

namespace MvcBasic
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<MvcBasicContext>(options => options.UseSqlite("Data Source=members.db"));
            services.AddControllersWithViews();
        }
(省略)
Models/Member.cs
using System;
using System.ComponentModel;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace MvcBasic.Models
{
  public class MvcBasicContext : DbContext
  {
        public MvcBasicContext (DbContextOptions options) : base(options) {}
        public DbSet<Member> Members { get; set; }
//        protected override void OnConfiguring(DbContextOptionsBuilder options)
//            => options.UseSqlite("Data Source=members.db");
  }
  public class Member
  {
    public int Id { get; set; }

    [DisplayName("氏名")]
    public string Name { get; set; }

    [DisplayName("メールアドレス")]
    public string Email { get; set; }

    [DisplayName("生年月日")]
    public DateTime Birth { get; set; }

    [DisplayName("既婚")]
    public bool Married { get; set; }

    [DisplayName("自己紹介")]
    public string Memo { get; set; }
  }
}
1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?