EF Core每次迁移都更新种子数据,而无需更改

编程入门 行业动态 更新时间:2024-10-25 14:24:00
本文介绍了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", 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", 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", 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", 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每次迁移都更新种子数据,而无需更改

本文发布于:2023-11-05 04:48:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:种子   数据   EF   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!