EF4 和 MVC3 将 ViewModel 映射到实体

发布于 2024-10-20 18:05:29 字数 1482 浏览 1 评论 0原文

我有一个 EF4 实体工作组。以下是该模型的元数据供参考。

    [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; }
}

我有一个 ViewModel 定义为:

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

在视图上我有一个网格来转储可用的工作组。这可行,但我想知道如何将 ID 字段转换为另一个表中的实际字符串。基本上,经理、customer_contact、lead 都是对联系人实体的引用。我想显示联系人中的姓名,而不仅仅是 ID。

如何才能做到这一点?我环顾四周,但似乎找不到建议或答案。也许我从错误的角度看待这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2024-10-27 18:05:29

您可能会考虑使用工作组(装饰器模式)或元组的包装器或创建将它们绑定在一起的自定义类。

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

在 EF 查询中,您可以执行以下操作:

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;

然后您的视图模型可以具有:

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

List<WorkgroupDisplayModel> Workgroups { get; set; }

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; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文