fetch the entire nested model in a single call EF6 MySQL

余生长醉 提交于 2020-01-16 08:21:07

问题


How we can fetch the entire model in a single fetch. Say model is

Model.MInner1.MInner2.MInner3

All the inner objects are List. I have only DbSet of Models and need to fetch entire model by loading all inner collection objects. I am getting this correctly with the statement below, also Object1 is loaded correctly.

List<Model> models = dbContext.Models.Include("MInner1.Object1")
                                     .Include("MInner1.MInner2.MInner3").ToList();

MInner3 collection contains 2 object that need to be loaded. Like loading object on MInner1, I change the Include to

.Include("MInner1.MInner2.MInner3.Object2").Include("MInner1.MInner2.MInner3.Object3")".

This gives wrong result. The MInner2 contains 5 MInner3 objects but only one is loading and that too with some other value. I can't tack it down. What is the correct way to load such nested models.

Edit

ClassStructure


public enum TypeStatus
{
    UnTouch = 0,
    Open,
    Completed,
    Paused,
    Discard
}
public class Project
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Details { get; set; }
    public int Status { get; set; }
    public List<PVersion> Versions { get; set; }
}
public class PVersion
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public TypePlatform Platform { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string Details { get; set; }
    public int Status { get; set; }
    public List<Scrum> Scrums { get; set; }
}
public class Scrum
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Details { get; set; }
    public int Status { get; set; }
    public List<Sprint> Sprints { get; set; }
}
public class Sprint
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public TypeSprint Type { get; set; }
    public float ExpectedTime { get; set; }
    public User User { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
    public DateTime? StartDate { get; set; }
    public string Details { get; set; }
    public int Status { get; set; }
    public List<SprintTime> SprintTimes { get; set; }
}
public class SprintTime
{
    [Key]
    public int ID { get; set; }
    public User User { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}
public class TypePlatform
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
}
public class TypeSprint
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
}
public enum TypeUser
{
    Admin = 1,
    Programmer,
    Tester
}
public class User
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string LoginIP { get; set; }
    public int UserType { get; set; }
}

public class IMDbContext : DbContext
{
    public DbSet<Project> Projects { get; set; }
    public DbSet<TypePlatform> TPlatforms { get; set; }
    public DbSet<TypeSprint> TSprints { get; set; }
    public DbSet<User> Users { get; set; }
}

Fetch Statement

List<Project> models = dbContext.Projects.Include("Versions.Platform").Include("Versions.Scrums.Sprints").Include("Versions.Scrums.Sprints.User").Include("Versions.Scrums.Sprints.Type").ToList();

来源:https://stackoverflow.com/questions/32738325/fetch-the-entire-nested-model-in-a-single-call-ef6-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!