EF Core每次迁移都更新种子数据,而无需更改 [英] EF Core Updates Seeded Data on every Migration without being changed

查看:78
本文介绍了EF Core每次迁移都更新种子数据,而无需更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用这样的用户和角色来播种数据库。

So I'm seeding my databse with Users and Roles like this.

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));
        }
    }

我正在创建迁移并更新数据库。到目前为止,一切都很好。

I'm creating a migration and Update the database. So far so good.

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

After that when I create another test migration, without changing anything it creates an UpdateData method with the same data. Everything in Up and Down methods is the same.

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 } });
    }
}
}

为什么不更新用户任何改变 ?我缺少什么?

Why it updates the Users without any change ? What am I missing ?

推荐答案

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

You have to seed with the raw value to avoid this :

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 = 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 },
                PasswordSalt = new byte[] { 112, 114, 101, 100, 101, 102, 105, 110, 101, 100, 32, 107, 101, 121 }
            },
            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 = 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 },
                PasswordSalt = new byte[] { 112, 114, 101, 100, 101, 102, 105, 110, 101, 100, 32, 107, 101, 121 }
            }
        };

这样,EF Core会在知道这是固定值的情况下生成迁移,并且不会每次更新移民。使用方法(此处为HashPassword)可以防止EF Core将其视为固定的。

That way EF Core will generate a migration knowing this is a fixed value and will not update it in every migration. Using a method (HashPassword here) prevent EF Core from considering it as fixed.

这篇关于EF Core每次迁移都更新种子数据,而无需更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆