Asp.net Core Mvc EF- Migrations使用

 Migragtion的命令,左邊是手動命令,右邊是代碼方式
html

 

 

首先來看命令方式:git

建立一個mvc項目,默認已經集成了EF包github

建立的項目包含了Microsoft.AspNetCore.Identity.EntityFramewordCore包,這將使用Entity Framework Core經過SQL Server來儲存身份識別的數據和表信息。sql

添加Sql鏈接字符串:數據庫

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "conn": "Data Source=pc;User ID=sa;Password=12;database=CoreDb;Connect Timeout=30;Encrypt=False;
TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
" } }

 

 

注意這個:ConnectionStrings 是必需要這樣寫的,默認是根據這個名字讀取,後面會說cookie

 

Net Core 身份 Identity 容許嚮應用中添加登錄功能。用戶可建立一個帳戶並進行登錄,登錄時可以使用用戶名、密碼mvc

Server數據庫存儲用戶名字、密碼和配置文件數據。另外,你可以使用其餘已有的存儲空間存儲數據app

建立ApplicationUser, ApplicationRole 分別繼承IdentityRole IdentityUser異步

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace IdentityDemo.Models
{
    public class ApplicationRole:IdentityRole
    {
    }
}
using Microsoft.AspNetCore.Identity;

namespace IdentityDemo.Models
{
    public class ApplicationUser : IdentityUser
    {
    }
}

 

 建立上下文類:async

using IdentityDemo.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace IdentityDemo.Date
{
  
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }
    }
}

 

 ConfigureServices 中註冊服務

public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
                {
                    options.LoginPath = "/Login";
                });

            services.AddDbContext<ApplicationDbContext>(options =>
            {
                //配置文件前面必須是;ConnectionStrings
                //由於默認是:GetSection("ConnectionStrings")[name].
                options.UseSqlServer(Configuration.GetConnectionString("conn"));
                //options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
            });

            services.AddIdentity<ApplicationUser, ApplicationRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            //配置密碼註冊方式
            services.Configure<IdentityOptions>(options =>
            {
                options.Password.RequireLowercase = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase = false;
                options.Password.RequiredLength = 1;
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

 

我這裏用了Cookie驗證,因此應該添加中間件  app.UseAuthentication();

 

建立用戶:經過依賴注入: UserManager 和 SignInManager 依賴:Microsoft.AspNetCore.Identity;

 private readonly UserManager<ApplicationUser> _userManager;
        private readonly SignInManager<ApplicationUser> _signinManager;
        public LoginController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signinManager)
        {
            _userManager = userManager;
            _signinManager = signinManager;
        }

 

 註冊代碼

[HttpPost]
        public async Task<IActionResult> Registe(LoginViewModel model)
        {
            var identityUser = new ApplicationUser
            {
                UserName = model.UserName,
                PasswordHash = model.PassWorld,
                NormalizedEmail = model.UserName
            };

            var identityResult = await _userManager.CreateAsync(identityUser, model.PassWorld);
            if (identityResult.Succeeded)
            {
                return RedirectToAction("Index", "Home");
            }
            return View();
        }

註冊以前,咱們先經過命令生成數據庫

命令依賴於:Microsoft.EntityFrameworkCore.Tools包,不過默認也有了

能夠輸入dotnet ef migrations --help 看看幫助

 

 

 輸入後命令:dotnet ef migrations add identity

 

 項目中會生成一個Migrations的文件夾和文件

 

 

 

 而後更新數據庫:dotnet ef migrations update

 能夠看到生成了數據庫和表,但AspNetUsers表的組建ID默認是字符串類型的,一連串GUID,待會會說改爲經常使用的int類型

 

 

 好了。如今註冊一個看看

 

看看默認的:

 

 接下來咱們修改默認的主鍵爲int類型。只須要改爲:IdentityUser<int> ,IdentityRole<int>

 上下文改爲:

 

由於是從新生成數據庫,須要刪除數據庫,而且把Migrations的文件都刪除,或者直接刪除Migrations文件夾

能夠執行命令刪除庫:dotnet ef database drop

不然EF會看成升級和降級處理,不會從新生成數據庫

 而後從新migragins add ,update 註冊看看,已是int類型了

 

 

而後給表升級,在用戶表添加一列 TrueName,首先看用戶名錶。默認是沒有該列的

 

咱們在ApplicationUser類新增字段

而後命令:dotnet ef migrations add AddTrueName  //AddTrueName名字是任意取的

此時在Migrations文件夾生成了對呀的類

 

 

而後update,這裏的update只會找最新的migrations 這裏的最新的migrations就是20190126061435_identity

數據庫已經有了

 

 

好比咱們在添加一個Address列,而後在還原到只有TrueName的狀態

 

 如今Migrations有3個Migration

 

而後咱們經過update回滾

dotnet ef database update 回滾的migration

咱們這裏回滾到AddTrueName 那麼命令就是:dotnet ef database update AddTrueName

 回滾以後。表也更新了

 

 

其實__EFMigrationsHistory表是保存了當前全部的Migration

回滾前

 

回滾後

 

 

 

這裏有兩個AddTrueName是由於我add了兩次,能夠不用管

主要是對比以前和以後,AddAddress沒有了

 

 而後看:dotnet ef migrations script 生成腳本

 

生成腳本在屏幕中,還得拷貝出來,多不友好,看看--help,是能夠指定路徑的

 

執行: dotnet ef migrations script -o d:\script\user.sql

 

 

 

 

打開生成的文件看看

 

 

 dotnet ef migratoins remove 是移除最新的一個migration,remove一次就移除一次,

前提是該migration沒有update到database,若是細心你會發現,當你add migration時候,會提示,能夠remove 移除

咱們添加3個 migration 分別是 temp01,temp02,temp03

 

 

 此時已經添加了3個測試的

 

 

記住,我這裏都沒有執行update的,因此在__EFMigrationsHistory也不會記錄。該表記錄的是已經update到database的migration

 

 

咱們執行一次命令:dotnet ef migrations remove

 由於temp03是最後添加的,因此先刪除,是否是像入棧出棧

 

 在remove一次,按照上面說的,應該是移除temp02

 

 

 

 就不一一測試了

來看看,若是update 後。能不能remove,上面已經還原到了TrueName

咱們先升級到Address

執行命令:dotnet ef database update Addaddress

 表字段和歷史紀錄也都有了

 

而後咱們執行命令:dotnet ef migrations remove

這裏要注意一點,咱們是加了Adress後,我爲了測試,又加了temp01,02,03,上面測試只remove了03,02

若是你沒有remove01的話,如今remove是remove的01,注意下就行了

如今咱們默認01,02,03都已經remove了。最新的確定就是address了

 

 好了,有點囉嗦。執行命令:dotnet ef migrations remove

 

 

info提示該migratin已經udate到database

經過dotnet ef migrations remove --help

能夠看到有其餘選項,有興趣的能夠自行研究

 

 

 

上面都是手動執行命令,咱們來看看在代碼裏面怎麼作

在程序初始化的時候,添加一個默認管理員的帳號,實例代碼以下

建立一個ApplicationDbContextSeed類,用於建立默認用戶

using IdentityDemo.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace IdentityDemo.Date
{
    public class ApplicationDbContextSeed
    {
        private UserManager<ApplicationUser> _userManger;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="server">依賴注入的容器,這裏能夠獲取依賴注入</param>
        /// <returns></returns>
        public async Task AsyncSpeed(ApplicationDbContext context, IServiceProvider server)
        {
            try
            {
                _userManger = server.GetRequiredService<UserManager<ApplicationUser>>();
                var logger = server.GetRequiredService<ILogger<ApplicationDbContext>>();
                logger.LogInformation("speed Init");

                //若是沒有用戶,則建立一個
                if (!_userManger.Users.Any())
                {
                    var defaultUser = new ApplicationUser
                    {
                        UserName = "Admin",
                        dEmail = "cnblogs@163.com"
                    };
                    var userResult = await _userManger.CreateAsync(defaultUser, "123456");
                    if(!userResult.Succeeded)
                    {
                        logger.LogError("建立失敗");
                        //logger.LogInformation("初始化用戶失敗");
                        userResult.Errors.ToList().ForEach(e => {
                            logger.LogError(e.Description);
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("初始化用戶失敗");
            }
        }
    }
}

 

添加一個對IWebHost擴展的類:

using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
namespace IdentityDemo.Date
{
    public static class WebHostMigrationExtensions
    {
        public static IWebHost MigrationDbContext<TContext>(this IWebHost host, Action<TContext, IServiceProvider> sedder) where TContext : DbContext
        {
            using (var scope = host.Services.CreateScope())
            {
                //拿到依賴注入容器
                var servers = scope.ServiceProvider;
                //var logger = servers.GetRequiredService<ILogger<TContext>>();
                var logger = servers.GetService<ILogger<TContext>>();
                var context = servers.GetService<TContext>();

                try
                {
                    context.Database.Migrate();
                    sedder(context, servers);
                    logger.LogInformation($"執行DbContex{typeof(TContext).Name}seed方法成功");
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, $"執行DbContex{typeof(TContext).Name}seed方法失敗");
                }
            }
            return host;
        }
    }
}

 

 

這樣就能夠在Program.cs的main方法中調用

 

 由於方法是異步方法,因此用Wait()方法改成同步的

而後刪除Migration中的全部文件,從新add,能夠 不update,由於代碼會:context.Database.Migrate();

而後數據庫已經初始化了

 

 經過var user =  _userManager.FindByEmailAsync("cnblogs@163.com").Result;能夠查找用戶,

 [HttpPost]
        public IActionResult Login(LoginViewModel model)
        {
           var user =  _userManager.FindByEmailAsync(model.Email).Result;

            if (user!=null)
            {
                var claims = new List<Claim> {
                    new Claim("name",model.UserName)
                };
                var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                var cliamsPrincipal = new ClaimsPrincipal(claimsIdentity);

                HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, cliamsPrincipal);

                return RedirectToAction("Index", "Home");
            }
            return RedirectToAction("Index");
        }

 

 

本文主要講了magration的操做和identity身份的驗證

 

源碼:https://github.com/byniqing/AspNetCore-identity

 

 

 參考資料

 

https://docs.microsoft.com/en-us/aspnet/core/migration/proper-to-2x/?view=aspnetcore-2.2

https://www.cnblogs.com/jqdy/p/5941248.html

https://docs.microsoft.com/zh-cn/ef/core/miscellaneous/cli/dotnet#dotnet-ef-migrations-remove

相關文章
相關標籤/搜索