ASP.NET Core Quick Start (Chapter 6: ASP.NET Core MVC) - Study Notes

Course Link: http://video.jessetalk.cn/course/explore

Conscience course, we work together to learn Ha!

Task 40: Introduction

Task 41: Individual authentication template

dotnet new mvc --help
Options:
  -au|--auth                      The type of authentication to use
                                      None             - No authentication
                                      Individual       - Individual authentication
                                      IndividualB2C    - Individual authentication with Azure AD B2C
                                      SingleOrg        - Organizational authentication for a single tenant
                                      MultiOrg         - Organizational authentication for multiple tenants
                                      Windows          - Windows authentication
                                  Default: None
  -uld|--use-local-db             Whether to use LocalDB instead of SQLite. This option only applies if --auth Individual or --auth IndividualB2C is specified.      
                                  bool - Optional
                                  Default: false / (*) true

Terminal garbled solve VScode

chcp 65001
dotnet new mvc -au Individual -uld --name IdentitySample

Created by default localdb, Identity

appsettings.json

  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-IdentitySample-40453418-3C8F-43D7-94F8-BD1BD20BDD96;Trusted_Connection=True;MultipleActiveResultSets=true"
  }

Startup.cs

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();

Initialize the database, create an updated database according to the database file Data / Migrations folder

dotnet ef database update

Error:

无法执行,因为找不到指定的命令或文件。
可能的原因包括:
  *你拼错了内置的 dotnet 命令。
  *你打算执行 .NET Core 程序,但 dotnet-ef 不存在。
  *你打算运行全局工具,但在路径上找不到名称前缀为 dotnet 的可执行文件。

Find a solution in stackoverflow:

https://stackoverflow.com/questions/45091909/dotnet-ef-database-update-no-executable-found-matching-command-dotnet-ef?r=SearchResults

Add a reference in ItemGroup csproj file

    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview2-final" />
dotnet restore
dotnet ef database update
dotnet run

Visit https: // localhost: 5001

Click Register to enter the registration page

Enter the mailbox password

Landed successfully

Click the mailbox into the Manage your account

Localdb connected via SSMS

dotnet run

Open PowerShell Gets an instance of the pipe name

& 'C:\Program Files\Microsoft SQL Server\130\Tools\Binn\SqlLocalDB.exe' info mssqllocaldb

Chinese PowerShell solve the garbage problem, check the UTF-8

By way of example the name of the connection pipe localdb

Task 42: EF Core Migration

New Database

Before add columns

New ApplicationUser.cs in the Models folder

using System;
using Microsoft.AspNetCore.Identity;

namespace IdentitySample.Models
{
    public class ApplicationUser : IdentityUser
    {
        public string NewColumn{get;set;}
    }
}
dotnet ef migrations add AddNewColumn

Automatically generated file

dotnet ef database update

After performing successfully refreshed database, the database can be seen in more than a NewColumn

New Address in the ApplicationUser.cs

        public string Address{get;set;}
dotnet ef migrations add AddAddress
dotnet ef database update

After performing successfully refreshed database, the database can be seen in more than one Address

Database Rollback

dotnet ef database update AddNewColumn

After performing successfully refreshed database, the database Address gone

dotnet ef migrations remove

After performing successfully removed AddAddress.cs and AddAddress.Designer.cs file

Generate sql script commands

dotnet ef migrations script

After the copy out of the database may be performed

Creative Commons License

This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing.

Welcome to reprint, use, repost, but be sure to keep the article signed by Zheng Ziming (containing links: http://www.cnblogs.com/MingsonZheng/), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification .

If you have any questions, please contact me ([email protected]).

Guess you like

Origin www.cnblogs.com/MingsonZheng/p/11623815.html