1

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();
abduIntegral
  • 521
  • 4
  • 7
  • 21
  • *only one is loading and that too with some other value* Are you 100% sure? Hard to believe that EF fails here. – Gert Arnold Sep 23 '15 at 11:41
  • yea 100%. All the inner model has variable Name. The value coming on that only one MInner3.Name is that of MInner1.Name. But this time Object2 and Object3 are loaded. Please help me on this. – abduIntegral Sep 23 '15 at 11:55
  • I have to see more details (class structure, mappings, EF version) to see what could possibly go wrong on here. – Gert Arnold Sep 23 '15 at 11:59
  • OK i will edit with the entire class structure. We are using EF6 with MySQL EF6, MySQL 6.9.7.0. When the code is run the database will create by its own with needed relations. So no extra mappings. – abduIntegral Sep 23 '15 at 12:14
  • Never seen anything like that occurring with SQL Server. Looks like it's a bug in the MySql query provider. Try to reduce the number of includes. – Gert Arnold Sep 25 '15 at 06:47
  • Yea If reduce number of includes i can fetch correctly. means .Include("Scrums.Sprints.User") will work to get version list. – abduIntegral Sep 25 '15 at 09:57
  • did you resolved this issue ? what was the solution. – Anchit Pancholi Aug 01 '17 at 03:17

0 Answers0