MVC NET 5链接模型保存到数据库但不加载

时间:2017-06-01 21:57:47

标签: c# asp.net-mvc asp.net-mvc-4 model

我是.NET MVC 5的新手,主要来自PHP背景。我有一个'项目'带有List集合的模型。当我只使用测试控制器将数据保存到集合中时,它将保存到数据库中。

当我试图从该集合中提取信息时会出现问题,因为它总是返回为null。

我的项目模型:

public class Project
{
    public int ID { get; set; }

    [Display(Name = "Project")]
    public string ProjectNumber { get; set; }

    [Display(Name = "Client")]
    public string Client { get; set; }

    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [Display(Name = "Start Date")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime StartDate { get; set; }

    [Display(Name = "Required Date")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime RequiredDate { get; set; }

    public string ProjectManagerId { get; set; }

    [ForeignKey("ProjectManagerId")]
    [Display(Name = "Project Manager")]
    public ApplicationUser ProjectManager { get; set; }

    [Display(Name = "Project Members")]
    public int[] ProjectMembers { get; set; }

    [Display(Name = "Tasks")]
    public List<ProjectTask> TaskCollection { get; set; }

}

public class ProjectDb : DbContext
{
    public ProjectDb() : base("DefaultConnection")
    {

    }

    public DbSet<Project> Projects { get; set; }
}

我的ProjectTask模型:     公共类ProjectTask     {

    public int Id { get; set; }

    public int ProjectId { get; set; }

    [ForeignKey("ProjectId")]
    public virtual Project Project { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public string TaskNotes { get; set; }

    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime StartDate { get; set; }

    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime DueDate { get; set; } 

    public int Duration { get; set; }

    public TaskStatus Status { get; set; }

    public TaskAlertLevel AlertLevel { get; set; }

    public List<ProjectTask> SubTasks { get; set; }

    public string Progress { get; set; }
}


public class ProjectTaskDb : DbContext
{
    public ProjectTaskDb() : base("DefaultConnection")
    {

    }

    public DbSet<Project> ProjectTasks { get; set; }
}

我的控制器,我只是想看到除空例外的其他内容:

public ActionResult DetailTasks(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        Project project = db.Projects.Find(id);

        if (project == null)
        {
            return HttpNotFound();
        }

        var tasks = project.TaskCollection;

        ViewBag.tasks = tasks;
        return View(project);

    }

希望它现在完全明显和愚蠢的东西逃脱了我!

2 个答案:

答案 0 :(得分:0)

您好我可以尝试更改此行:var tasks = project.TaskCollection;这样:var tasks = project.TaskCollection.ToList();,它可能对您有所帮助

完整示例/更改代码:

public ActionResult DetailTasks(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        Project project = db.Projects.Find(id);

        if (project == null)
        {
            return HttpNotFound();
        }

        var tasks = project.TaskCollection.ToList();

        ViewBag.tasks = tasks;
        return View(project);

    }

我相信这次你不会为project.TaskCollection获取null,如果数据库中有可用的数据。

希望以上信息有用,请让我知道您的想法或反馈

由于

KARTHIK

答案 1 :(得分:0)

关于评论的道歉,出于某种原因我认为你在加载初始项目对象时遇到了问题。因此,查看您的项目实体,您关闭了延迟加载,因为您没有使用虚拟关键字:

public List<ProjectTask> TaskCollection { get; set; }

VS

public virtual List<ProjectTask> TaskCollection { get; set; }

执行此操作时,您必须显式或急切地加载TaskCollection

明确加载:

Project project = db.Projects.Find(id);
db.Entry(project).List(a => a.TaskCollection).Load();

急切地加载:

Project project = db.Projects.where(a => a.ID == id).Include(b => b.TaskCollection).FirstOrDefault();

P.S。你可能不得不玩我的上下文,因为我不在我可以加载VS的地方