ASP.NET Core快速入门(第6章:ASP.NET Core MVC)--学习笔记

课程链接:http://video.jessetalk.cn/course/explore

良心课程,大家一起来学习哈!

任务40:介绍

任务41:Individual authentication 模板

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

解决VScode终端乱码

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

默认创建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>();

初始化数据库,根据Data/Migrations文件夹下的数据库文件创建更新数据库

dotnet ef database update

报错:

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

在stackoverflow找到解决方法:

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

在csproj文件的ItemGroup中添加引用

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

访问https://localhost:5001

点击Register进入注册页面

输入邮箱密码登陆

登陆成功

点击邮箱进入Manage your account

通过SSMS连接localdb

dotnet run

打开PowerShell获取实例管道名称

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

解决PowerShell中文乱码问题,勾选UTF-8

通过实例管道名称连接localdb

任务42:EF Core Migration

数据库新增

添加列之前

在Models文件夹下新增ApplicationUser.cs

using System;
using Microsoft.AspNetCore.Identity;

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

自动生成文件

dotnet ef database update

执行成功后刷新数据库,可以看到数据库中多了一列NewColumn

在ApplicationUser.cs中新增Address

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

执行成功后刷新数据库,可以看到数据库中多了一列Address

数据库回滚

dotnet ef database update AddNewColumn

执行成功后刷新数据库,可以看到数据库中Address不见了

dotnet ef migrations remove

执行成功后移除AddAddress.cs以及AddAddress.Designer.cs文件

生成sql脚本命令

dotnet ef migrations script

拷贝出来后可在数据库执行

知识共享许可协议

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

如有任何疑问,请与我联系 ([email protected]) 。

猜你喜欢

转载自www.cnblogs.com/MingsonZheng/p/11623815.html