如何在viewmodel中再次提及它时,将entityobject的数据注释添加到viewmodel对象

时间:2011-12-22 08:32:05

标签: asp.net-mvc-3 viewmodel automapper

我一直遇到一个问题,我必须在我的viewmodel上使用注释

例如我的entityobject是设计器类中的User(Teamwork.Designer.cs)

假设我在User_Name和User_Password

上写了数据注释
 [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Int32 User_Pk
    {
        get
        {
            return _User_Pk;
        }
        set
        {
            if (_User_Pk != value)
            {
                OnUser_PkChanging(value);
                ReportPropertyChanging("User_Pk");
                _User_Pk = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("User_Pk");
                OnUser_PkChanged();
            }
        }
    }
    private global::System.Int32 _User_Pk;
    partial void OnUser_PkChanging(global::System.Int32 value);
    partial void OnUser_PkChanged();


    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    [Required(ErrorMessage="Please enter your name")]
    [StringLength(20,ErrorMessage="Name cannot exceed 20 characters")]
    [RegularExpression(@"^([a-zA-Z0-9 \.\&\'\-]+)$", ErrorMessage = "Invalid name")]
    public global::System.String User_Name
    {
        get
        {
            return _User_Name;
        }
        set
        {
            OnUser_NameChanging(value);
            ReportPropertyChanging("User_Name");
            _User_Name = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("User_Name");
            OnUser_NameChanged();
        }
    }
    private global::System.String _User_Name;
    partial void OnUser_NameChanging(global::System.String value);
    partial void OnUser_NameChanged();


    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    [Email(ErrorMessage="Invalid email address")]
    [Required(ErrorMessage="Please enter email address")]
    public global::System.String User_Mail_Id
    {
        get
        {
            return _User_Mail_Id;
        }
        set
        {
            OnUser_Mail_IdChanging(value);
            ReportPropertyChanging("User_Mail_Id");
            _User_Mail_Id = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("User_Mail_Id");
            OnUser_Mail_IdChanged();
        }
    }
    private global::System.String _User_Mail_Id;
    partial void OnUser_Mail_IdChanging(global::System.String value);
    partial void OnUser_Mail_IdChanged();


    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    [Required(ErrorMessage="Please enter password")]
    [StringLength(20,ErrorMessage="Password cannot exceed 20 characters")]
    [RegularExpression(@"^([a-zA-Z0-9 \.\&\'\-]+)$", ErrorMessage = "Invalid password")]
    public global::System.String User_Password
    {
        get
        {
            return _User_Password;
        }
        set
        {
            OnUser_PasswordChanging(value);
            ReportPropertyChanging("User_Password");
            _User_Password = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("User_Password");
            OnUser_PasswordChanged();
        }
    }
    private global::System.String _User_Password;
    partial void OnUser_PasswordChanging(global::System.String value);
    partial void OnUser_PasswordChanged();


    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.DateTime User_Creation_Date
    {
        get
        {
            return _User_Creation_Date;
        }
        set
        {
            OnUser_Creation_DateChanging(value);
            ReportPropertyChanging("User_Creation_Date");
            _User_Creation_Date = StructuralObject.SetValidValue(value);
            ReportPropertyChanged("User_Creation_Date");
            OnUser_Creation_DateChanged();
        }
    }
    private global::System.DateTime _User_Creation_Date;
    partial void OnUser_Creation_DateChanging(global::System.DateTime value);
    partial void OnUser_Creation_DateChanged();


    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Boolean Is_Active
    {
        get
        {
            return _Is_Active;
        }
        set
        {
            OnIs_ActiveChanging(value);
            ReportPropertyChanging("Is_Active");
            _Is_Active = StructuralObject.SetValidValue(value);
            ReportPropertyChanged("Is_Active");
            OnIs_ActiveChanged();
        }
    }
    private global::System.Boolean _Is_Active;
    partial void OnIs_ActiveChanging(global::System.Boolean value);
    partial void OnIs_ActiveChanged();

    #endregion

现在我已经为上面的User:EntityObject创建了一个viewmodel 名称为RegisterUserViewModel.cs,如下所示

 public class RegisterUserViewModel
 {
   public int UserId{get;set;}
   public string UserName { get; set; }
   public string UserEmail { get; set; }
   public string UserPassword { get; set;}
 }

实体对象在名为Teamwork.Data的单独文件夹中创建 以及在Teamwork.BusinessLogic文件夹

中创建的viewmodel

现在在我的控制器动作中我调用了一个方法,该方法在Teamwork.Businesslogic中定义如下

public ViewResult Register(string invite)
    {
            SignUpAndLogin signLog = new SignUpAndLogin();
            var model = signLog.GetRegisterModel();
            return View(model);

    }

SignUpLogin.cs是包含Teamwork.BusinessLogic文件夹中所有与业务相关的操作的类,其中GetRegisterModel如下所示

  public RegisterUserViewModel GetRegisterModel()
    {
        AutoMapper.Mapper.CreateMap<User, RegisterUserViewModel>();
        User user = new User();
        RegisterUserViewModel model = AutoMapper.Mapper.Map<User,      RegisterUserViewModel>(user);
        return model;
    }'

我已将RegisterviewModel对象与User对象映射,并将其返回到调用上述方法的操作。(请问有人能说这是正确的映射方式吗?)

我的问题是我必须在RegisterViewModel中再次提及注释(即使在映射之后?),如下所示,注释可以工作

 public class RegisterUserViewModel
 {
   public int UserId{get;set;}
   [Required(ErrorMessage="Enter the Name")]
   public string UserName { get; set; }
   public string UserEmail { get; set; }
   public string UserPassword { get; set;}
 }

任何人都可以说我的映射是否正确以及避免在视图模型中重复数据注释的任何方法

问题很长,可能很愚蠢......但这是我现在面临的问题2天了... 任何人都可以帮忙

1 个答案:

答案 0 :(得分:2)

我认为您不应该使用自定义代码编辑Teamwork.Designer.cs。如果您对数据库进行更改,然后重新生成Teamwork.edmx,那么该文件很可能会被覆盖。

只需将数据注释添加到视图模型中即可。

Automapper映射似乎会给你一个错误,因为源和目标对象之间的属性名称不同。

Mapper.CreateMap<User, RegisterUserViewModel>()
.ForMember(destinationMember => destinationMember.UserName, memberOptions => memberOptions.MapFrom(sourceMember => sourceMember.User_Name)
.ForMember(destinationMember => destinationMember.UserPassword, memberOptions => memberOptions.MapFrom(sourceMember => sourceMember.User_Password)
.ForMember(destinationMember => destinationMember.UserEmail, memberOptions => memberOptions.MapFrom(sourceMember => sourceMember.User_Mail_Id)
.ForMember(destinationMember => destinationMember.UserId, memberOptions => memberOptions.MapFrom(sourceMember => sourceMember.User_Pk);
Mapper.AssertConfigurationIsValid();