1

can anybody provide a link for creating a viewmodel for the edmx designer class

suppose my edmx file is named School.edmx and it has school.Designer.cs class. In the designer class i have the folowing entity object

[EdmEntityTypeAttribute(NamespaceName="teamworkModel", Name="User")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class User : EntityObject
{

    #region Primitive Properties

    [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();

I have the following columns in the above entity object (User table)User_PK,User_Name,User_Password,User_Email_ID.....

Please can anyone suggest how to create a viewmodel for the above entity object that contains all the above columns except User_Password and User_Email_ID because i need to use it as strongly typed viewmodel for my view.I also need to use another table in the same viewmodel with selected columns....

i had gone through a lot of documents..i already spent 1 and half day for this can anybody help.. i know this question is asked repeatedly but i cannt find the right way in doing it... Thanks

bhargav
  • 619
  • 2
  • 14
  • 30

3 Answers3

1

Edit: Modified to answer comment about extra properties from other Entities

This may help

public class UserViewModel
{
public int Pk{get;private set;}
public string Name{get;set;}
public DateTime CreationDate{get;set;}
public string ProjectName{get;set;}
public DateTime ProjectCreated{get;set;}
}

The ViewModel is a flattened version of you entities.

heads5150
  • 7,263
  • 3
  • 26
  • 34
  • thanks but how do i include other entity objects with selected columns in the same viewmodel..suppose i have projects table where i have to include projectname and projectcreated date columns and how do i map it... – bhargav Dec 21 '11 at 11:11
  • i think i wil have multiple projects so i should use List for projectname.? and can u give a short example on using the mapping for my situation – bhargav Dec 21 '11 at 11:19
  • var model = new User{User_Pk=userViewModel.Pk....} var model2 = new Project{Name=userViewModel.Name...} – heads5150 Dec 21 '11 at 11:23
  • thanks a lot..i was actually looking for automapper to be used...can u suggest a good link for my scenario – bhargav Dec 21 '11 at 11:39
  • Have a look at the automapper wiki Jimmy Bogard has got great documentation [Automapper Wiki](https://github.com/AutoMapper/AutoMapper/wiki) – heads5150 Dec 21 '11 at 20:57
1

You have to write your viewmodel classes yourself. Just create a class called something like MyWebApp.Models.User which contains the properties you need and map the edmx class to it in your controller.

For mapping i recommend using the automapper which makes mapping life easier.

EDIT This is how you use automapper: Say your DAL class is called User and your viewmodel class is called UserViewModel.

You have to tell automapper once which classes you want to map:

AutoMapper.Mapper.CreateMap<User, UserViewModel>();

Then you can map objects from one type to another with a single line:

User user = dal.GetUser();
UserViewModel model = AutoMapper.Mapper.Map<User, UserViewModel>(user);

Automapper can even map collections of objects. So if your model has a property like IEnumerable<Projects> it will map this as well.

Jan
  • 15,802
  • 5
  • 35
  • 59
0

I get VS to do most of the work for me by right clicking on the edmx design surface and selecting Add Code Generation Item > EF 5.x DBContext Generator > Add.

You should then see a [xxx].tt file in the same folder as your edmx file. If you expand this file you should see a POCO class for each of your entities.

I copy the entities as required into my viewmodel and delete the ones I don't need.

Once I'm happy with my viewmodel I delete the [xx].tt file, right click properties on the edmx design surface > Properties > Code Generation strategy > default.

I'd also highly recommend using Automapper as Jan mentioned in his answer.

woggles
  • 7,444
  • 12
  • 70
  • 130