我需要一些帮助来了解 DTO 和扩展方法如何处理 EF5 Code First MVC4 中的多对多关系。
我有以下 DTO
using System;
using System.Collections.Generic;
namespace Mirtol.Web.Models
{
public class TaskDetail
{
public DateTime DueDate {get;set;}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Status { get; set; }
public string Phase { get; set; }
public string Project { get; set; }
public DateTime StartDate { get; set; }
public string overdue { get; set; }
public List<GroupDetail> Groups { get; set; }
}
}
using System;
namespace Mirtol.Web.Models
{
public class GroupDetail
{
public string GroupName {get;set;}
public string GroupDescription { get; set; }
}
}
除此之外,我还有以下扩展;
using Mirtol.Web.Models;
using System.Collections;
using System.Collections.Generic;
namespace Mirtol.Web.Extensions
{
public static class TaskDetailExtensions
{
public static TaskDetail ToTaskDetail(this Task task)
{
var success = "success";
var warning = "warning";
var error = "error";
return new TaskDetail
{
Id = task.Id,
Name = task.Name,
Description = task.Description,
Status = task.Status,
Phase = task.Phase != null ? task.Phase.Name : string.Empty,
Project = task.Project != null ? task.Project.ShortName : string.Empty,
StartDate = task.StartDateTime,
DueDate = task.DueDate,
overdue = task.DueDate > System.DateTime.Now ? success : task.DueDate < System.DateTime.Now.AddDays(-7) ? error : warning,
};
}
}
}
using Mirtol.Entities.Mir;
using Mirtol.Web.Models;
namespace Mirtol.Web.Extensions
{
public static class GroupDetailExtensions
{
public static GroupDetail ToGroupDetail(this Group group)
{
return new GroupDetail
{
GroupName = group.Name != null ? group.Name: string.Empty,
GroupDescription = group.Description !=null ? group.Description: string.Empty,
};
}
}
}
所以我的任务控制器有一个动作;
public IEnumerable<TaskDetail> GetUserProjectTasks(int id, string ustr, int uid)
{
var projectUTasks = taskRepository.GetUserProjectTasks(id, ustr, mirtolSecurity.CurrentUserId).Distinct();
return projectUTasks.OrderByDescending(x => x.DueDate).Select(x => x.ToTaskDetail());
}
返回到TaskDetail。并包括“组”
我正在努力解决的是在TaskDetailExtension和TaskDetaail中,我如何反映一个任务可以属于许多组并且组可以有很多任务的事实?CF 在我的实体类上处理了这个问题,并且关系似乎在那里设置得很好。
我的想法类似于 Groups = task.Groups(x => x.Groups.ToGroupDetail()),
在 TaskDetailExtension 中?
任何帮助表示赞赏。
约翰