【问题标题】:EF Code First and confirmation fieldsEF Code First 和确认字段
【发布时间】:2012-12-25 15:19:04
【问题描述】:

我正在创建一个用户类并想添加一个 ConfirmPassword 字段。由于我的数据库中没有此字段,我该如何处理?我也在使用 AutoMapper,我是否需要做任何事情来处理这个额外的字段,例如告诉映射器忽略这个字段?

我在这里有我的用户类,我刚刚在好友类中添加了 NotMapped 属性。这就是我需要做的吗?或者是否需要任何额外的编码来处理这种情况?

public partial class User
{
    public User()
    {
        //this.DateCreated = DateTime.Now; //set default value
        Roles = new HashSet<Role>();
    }

    public ICollection<Role> Roles { get; set; } //many to many relationship

    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string ConfirmPassword { get; set; }
    public string City { get; set; }

    //foreign key
    public int CountryId { get; set; }
    //navigation properties
    public virtual Country Country { get; set; }

    //foreign key
    public int LanguageId { get; set; }
    //navigation properties
    public virtual Language Language { get; set; }

    public string EmailAddress { get; set; }
    public long FacebookId { get; set; }
    public DateTime DateCreated { get; set; }
}

//buddy class, validation in here because not supported in Fluent API
//test in ie
//MetadataType decorated class
[MetadataType(typeof(UserMetadata))]
public partial class User
{
}

//Metadata type
internal sealed class UserMetadata
{
    [Required]
    public string FirstName { get; set; }

    [Required]
    public string Surname { get; set; }

    [Required]
    [Remote("IsUsernameAvailable", "Validation")]
    [DataType(DataType.Text)]
    [DisplayName("Username")]
    public string Username { get; set; }

    [Required]
    public int CountryId { get; set; }

    public string Password { get; set; }

    [NotMapped]
    public string ConfirmPassword { get; set; }
    public string City { get; set; }
    public int LanguageId { get; set; } 
    public string EmailAddress { get; set; }
    public long FacebookId { get; set; }
    public DateTime DateCreated { get; set; }
}

}

编辑:这是我对应的 DTO 类:

public class UserDTO
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string City { get; set; }
        public string CountryName { get; set; }
        public string LanguageName { get; set; }
        public string EmailAddress { get; set; }
        public long FacebookId { get; set; }
        public DateTime DateCreated { get; set; }
}

【问题讨论】:

    标签: asp.net-mvc entity-framework automapper


    【解决方案1】:

    ConfirmPassword 字段应该在您的视图模型类上定义,而不是在实体框架跟踪的域模型对象上定义。由于该值从未存储到数据库中,因此它甚至不应该成为您的域模型的一部分。

    此属性应仅在您的视图模型类上定义,该类映射到您的创建帐户视图。您不需要使用 AutoMapper 执行任何特殊步骤来处理此字段,因为它不应该是您的域模型的一部分 => 当 AutoMapper 在您的 CreateUserViewModel 和您的用户模型之间进行映射时,它将被忽略。

    【讨论】:

    • 我正在使用 DTO 和伙伴类进行验证。我将如何实施您的建议,因为 ConfirmPassword 属性需要在类中进行验证。
    • 不,ConfirmPassword 属性,正如我在回答中已经解释的那样,必须仅位于视图模型上。然后,您将验证此视图模型。验证视图模型后,您可以使用 AutoMapper 构建您的 DTO 或域模型,或者您从视图模型中调用它的任何内容。但是再一次,ConfirmPassword 字段只对视图模型有意义。如果你把它放在你的模型的其他一些层,恐怕你做错了。
    • 我对模型与视图模型有点混淆......我的模型与数据库交互,然后是在 UI 端使用的 DTO。视图模型在哪里出现?
    • NotMapped 属性意味着我可以将属性放入我的验证伙伴类中,但将其排除在我的模型之外,这不是解决这个问题的方法吗?
    • NotMapped 属性是实体框架特定的属性。正如我在回答中已经说过的那样, ConfirmPassword 属性应该在您的视图模型上定义,而不是在您的域模型上。视图模型上不应有任何持久的特定属性。有很多关于视图模型使用的教程。这是一个:rachelappel.com/…
    猜你喜欢
    • 1970-01-01
    • 2012-11-27
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多