简体   繁体   中英

EF4 and MVC3 mapping ViewModels to Entities

I have an EF4 Entity Workgroup. Below is the meta-data for that model for reference.

    [MetadataType(typeof(WorkgroupMetaData))]
public partial class Workgroup {
    public Contact manager { get; set; }
}

[Bind(Exclude = "id")]
public class WorkgroupMetaData
{
    [ScaffoldColumn(false)]
    public int id { get; set; }

    [DisplayName("Org. Number")]
    [Required(ErrorMessage = "Org. Number is required.")]
    public string org_number { get; set; }

    [DisplayName("Workgroup Name")]
    [Required(ErrorMessage = "Workgroup name is required.")]
    public string name { get; set; }

    [DisplayName("Customer Contact")]
    public int customer_contact_id { get; set; }

    [DisplayName("Manager")]
    public int manager_id { get; set; }

    [DisplayName("Tech. Lead")]
    public int lead_id { get; set; }

    [DisplayName("Time Approver")]
    public int time_approver { get; set; }

    [DisplayName("Description")]
    public string description { get; set; }

    [ScaffoldColumn(false)]
    public object created_at { get; set; }

    [ScaffoldColumn(false)]
    public object last_modified_at { get; set; }
}

I've got a ViewModel defined as:

    public class WorkgroupViewModel
{
    public Workgroup Workgroup { get; set; }
    public List<Workgroup> Workgroups { get; set; }
}

On the view I have a grid to dump out the workgroups available. This works but I was wondering how to convert the ID fields to the actual strings from another table. Basically the manager, customer_contact, lead are all references to the Contact entity. I would like to show the names from Contacts instead of just the id.

How can this be accomplished? I've looked around a bit but I can't seem to find a suggestion or an answer. Maybe I looking at this from the wrong perspective?

You might consider using a wrapper around Workgroup (decorator pattern) or Tuple or creating a custom class that binds them together.

public class WorkgroupDisplayModel
{
    public Workgroup Workgroup { get; set; }
    public Manager Manager { get; set; }
    // Add additional properties for each related type
}

In your EF query you can do something like:

var query = from w in Context.Workgroups
            join m in Context.Managers
                on w.manager_id equals m.uid
            // Additional joins for each related table
            where w.Description == "Project 1" // Whatever criteria
            select Tuple.Create(w, m); // Add param for each type
            //or
            //select new WorkgroupDisplayModel { Workgroup = w, Manager = m, ... };
var list = query.ToList();
var contact = list[0].Item1; // Tuple has strongly typed Item1 thru ItemN
var manager = list[0].Item2;

Then your view model could have:

List<Tuple<Workgroup, Manager, Customer, Lead>> Workgroups { get; set; }

or

List<WorkgroupDisplayModel> Workgroups { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM