在单个调用EF6 MySQL中获取整个嵌套模型

时间:2015-09-23 11:27:01

标签: mysql entity-framework asp.net-mvc-4 collections

我们如何在一次获取中获取整个模型。 说模型是

Model.MInner1.MInner2.MInner3

所有内部对象都是List。我只有DbSet模型,需要通过加载所有内部集合对象来获取整个模型。我正确地使用下面的语句得到了这一点,也正确加载了Object1。

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

MInner3集合包含2个需要加载的对象。就像在MInner1上加载对象一样,我将Include更改为

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

这给出了错误的结果。 MInner2包含5个MInner3对象,但只有一个正在加载,而且还有一些其他值。我无法解决这个问题。加载此类嵌套模型的正确方法是什么。

修改

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

获取声明

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

0 个答案:

没有答案