EF核心每次迁移均更新种子数据,而无需更改

时间:2019-03-13 22:06:10

标签: c# .net-core entity-framework-core ef-migrations ef-core-2.0

因此,我正在将像这样的用户和角色作为数据库的种子。

public static void SeedUsers(this ModelBuilder modelBuilder)
    {
        var roles = new[]
        {
            new Role
            {
                Id = new Guid("5127599a-e956-4f5e-9385-1b8c6a74e4f1"),
                RoleName = "Customer"
            },
            new Role
            {
                Id = new Guid("8634c476-20fa-4391-b8f7-8713abf61af0"),
                RoleName = "Admin"
            }
        };

        // customer password
        byte[] customerPasswordHash = null;
        byte[] customerPasswordSalt = null;
        HashPassword("asd123", out customerPasswordHash, out customerPasswordSalt);

        // admin password
        byte[] adminPasswordHash = null;
        byte[] adminPasswordSalt = null;
        HashPassword("asd123", out adminPasswordHash, out adminPasswordSalt);
        var users = new[]
        {
            new Users()
            {
                Id = new Guid("3b86f5a2-1978-46e3-a0b6-edbb6b558efc"),
                FirstName = "John",
                LastName = "Doe",
                DepositedCash = 10000,
                Email = "john@doe.com",
                RoleId = roles[0].Id,
                PasswordHash = customerPasswordHash,
                PasswordSalt = customerPasswordSalt
            },
            new Users()
            {
                Id = new Guid("c643b944-53d9-4a0c-9922-3486558b9129"),
                FirstName = "Admin",
                LastName = "Admin",
                DepositedCash = 10000,
                Email = "admin@admin.com",
                RoleId = roles[1].Id,
                PasswordHash = adminPasswordHash,
                PasswordSalt = adminPasswordSalt
            }
        };

        modelBuilder.Entity<Role>()
            .HasData(roles);

        modelBuilder.Entity<Users>()
            .HasData(users);
    }

    private static void HashPassword(string password, out byte[] passwordHash, out byte[] passwordSalt)
    {
        using (var hmac = new System.Security.Cryptography.HMACSHA512(Encoding.UTF8.GetBytes("predefined key")))
        {
            passwordSalt = hmac.Key;
            passwordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password));
        }
    }

我正在创建迁移并更新数据库。到目前为止一切顺利。

之后,当我创建另一个测试迁移时,不更改任何内容,它将创建具有相同数据的UpdateData方法。 UpDown方法中的所有内容都相同。

using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Pizzeria.Data.Migrations
{
public partial class test : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.UpdateData(
            table: "Users",
            keyColumn: "Id",
            keyValue: new Guid("3b86f5a2-1978-46e3-a0b6-edbb6b558efc"),
            columns: new[] { "PasswordHash", "PasswordSalt" },
            values: new object[] { new byte[] { 211, 141, 232, 166, 77, 237, 128, 211, 121, 45, 47, 68, 211, 202, 162, 3, 39, 214, 212, 153, 4, 199, 223, 213, 43, 233, 224, 21, 107, 10, 62, 220, 105, 27, 254, 137, 16, 208, 56, 42, 254, 211, 154, 27, 60, 135, 197, 224, 148, 99, 232, 246, 220, 121, 113, 103, 42, 117, 172, 240, 134, 14, 188, 25 }, new byte[] { 112, 114, 101, 100, 101, 102, 105, 110, 101, 100, 32, 107, 101, 121 } });

        migrationBuilder.UpdateData(
            table: "Users",
            keyColumn: "Id",
            keyValue: new Guid("c643b944-53d9-4a0c-9922-3486558b9129"),
            columns: new[] { "PasswordHash", "PasswordSalt" },
            values: new object[] { new byte[] { 211, 141, 232, 166, 77, 237, 128, 211, 121, 45, 47, 68, 211, 202, 162, 3, 39, 214, 212, 153, 4, 199, 223, 213, 43, 233, 224, 21, 107, 10, 62, 220, 105, 27, 254, 137, 16, 208, 56, 42, 254, 211, 154, 27, 60, 135, 197, 224, 148, 99, 232, 246, 220, 121, 113, 103, 42, 117, 172, 240, 134, 14, 188, 25 }, new byte[] { 112, 114, 101, 100, 101, 102, 105, 110, 101, 100, 32, 107, 101, 121 } });
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.UpdateData(
            table: "Users",
            keyColumn: "Id",
            keyValue: new Guid("3b86f5a2-1978-46e3-a0b6-edbb6b558efc"),
            columns: new[] { "PasswordHash", "PasswordSalt" },
            values: new object[] { new byte[] { 211, 141, 232, 166, 77, 237, 128, 211, 121, 45, 47, 68, 211, 202, 162, 3, 39, 214, 212, 153, 4, 199, 223, 213, 43, 233, 224, 21, 107, 10, 62, 220, 105, 27, 254, 137, 16, 208, 56, 42, 254, 211, 154, 27, 60, 135, 197, 224, 148, 99, 232, 246, 220, 121, 113, 103, 42, 117, 172, 240, 134, 14, 188, 25 }, new byte[] { 112, 114, 101, 100, 101, 102, 105, 110, 101, 100, 32, 107, 101, 121 } });

        migrationBuilder.UpdateData(
            table: "Users",
            keyColumn: "Id",
            keyValue: new Guid("c643b944-53d9-4a0c-9922-3486558b9129"),
            columns: new[] { "PasswordHash", "PasswordSalt" },
            values: new object[] { new byte[] { 211, 141, 232, 166, 77, 237, 128, 211, 121, 45, 47, 68, 211, 202, 162, 3, 39, 214, 212, 153, 4, 199, 223, 213, 43, 233, 224, 21, 107, 10, 62, 220, 105, 27, 254, 137, 16, 208, 56, 42, 254, 211, 154, 27, 60, 135, 197, 224, 148, 99, 232, 246, 220, 121, 113, 103, 42, 117, 172, 240, 134, 14, 188, 25 }, new byte[] { 112, 114, 101, 100, 101, 102, 105, 110, 101, 100, 32, 107, 101, 121 } });
    }
}
}

为什么更新用户而不做任何更改?我想念什么?

1 个答案:

答案 0 :(得分:1)

您必须使用原始值作为种子,以避免这种情况:

site1(url).then(data=>{
     console.log(data);
   })

这样,EF Core会知道该值是固定值,从而生成一个迁移,并且不会在每次迁移中都进行更新。使用一种方法(此处为HashPassword)可以防止EF Core将其视为固定的。