gpt4 book ai didi

c# - 使用 EF 代码首先,如何自动填充 HashedPassword 列?

转载 作者:行者123 更新时间:2023-11-30 22:19:55 24 4
gpt4 key购买 nike

我有这个实体:

namespace FP.Domain.Entities
{
public class Admins
{
public int ID { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string HashedPassword { get; set; }
}
}

我已经使用 Fluent API 忽略了实体配置中的 Password 属性,因此它不会被映射。

但我希望在创建新记录或更新记录时自动填充 HashedPassword

HashedPassword 值取自 Password 属性的散列。

每次我创建新记录或更新现有记录的 Password 时,我真的无法理解要自动填充 HashedPassword。因此,我们将不胜感激。

最佳答案

将代码放入密码 setter 中。

例如

public class Admins
{
public int ID { get; set; }
public string Username { get; set; }
public string HashedPassword { get; set; }

private string _password;
public string Password
{
get { return _password; }
set
{
_password = value;
HashedPassword = HashingMethod(_password);
}
}
}

您甚至可能不需要 setter/getter 。

关于c# - 使用 EF 代码首先,如何自动填充 HashedPassword 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15304115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com