2

ユーザー クラスを作成していて、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; }
}
4

1 に答える 1

2

ConfirmPasswordフィールドは、Entity Framework によって追跡されるドメイン モデル オブジェクトではなく、ビュー モデル クラスで定義する必要があります。この値はデータベースに保存されることはないため、ドメイン モデルの一部であってはなりません。

このプロパティは、アカウントの作成ビューにマップされているビュー モデル クラスでのみ定義する必要があります。このフィールドを処理するために AutoMapper で特別な手順を実行する必要はありません。これは、ドメイン モデルの一部であってはならないためです => AutoMapper が CreateUserViewModel と User モデルの間でマッピングを行っている場合は無視されます。

于 2012-12-25T15:22:09.183 に答える