Entity Framework - nested entites are null

asp.net asp.net-mvc c# entity-framework entity-framework-6

Question

I have one-to-many relationship, which I designed using this. and I use Code-First approach.

i.e.:

public class JobData
{
    [Key]
    public int JobDataId { get; set; }
    public int JobId { get; set; }
    ...
    public virtual ICollection<TaskInfo> TaskInfoes { get; set; }
}

Where TaskInfo is defined like this:

public class TaskInfo
{
    public int TaskInfoId { get; set; }
    public int ExecId { get; set; }

    public virtual FrameData FrameData { get; set; }
    public virtual Card Card { get; set; }
    public virtual Tags Tags { get; set; }
    public virtual DriverInfo DriverInfo { get; set; }
    public virtual JobData JobData { get; set; }
}

FrameData, Card, Tags, DriverInfo are all one-to-one entites with TaskInfo:

public class DriverInfo
{       
    public int DriverId { get; set; }

    [ForeignKey("TaskInfo")]
    public int DriverInfoId { get; set; }
    public virtual TaskInfo TaskInfo { get; set; }
}

Tables are created just fine with this approach, I get JobData table and TaskInfoes table, that hold reference to JobData and I also have tables for each nested class in TaskInfo. The data is even saved correctly, thus, every table is populated. However, when I'm trying to get jobData instance, it has entire list of TaskInfoes, but all nested classes are unfortunately null. What am i doing wrong here? I have been trying different solutions, like specifying [Key,ForeignKey("") in nested classes, tried creating separate context for TaskInfoes and use something like this:

modelBuilder.Entity<TaskInfo>()               
            .HasRequired<JobData>(s => s.JobData) // TaskInfo entity requires JobData 
            .WithMany(s => s.TaskInfoes)        // JobData entity includes many TaskInfoes entities
            .WillCascadeOnDelete(true);

modelBuilder.Entity<TaskInfo>()
            .HasRequired<FrameData>(s => s.FrameData);// TaskInfo entity requires FrameData 

As well as:

var data = context.TaskInfoes
        .Include(s => s.Framedata)
        .Include(s => s.Card)
        .Include(s => s.DriverInfo)
        .Include(s => s.Tags)
        .ToList();

Nothing works, nested classes are null no matter what. I'm totally missing something important here, could you please tell me what ? :)

P.S. the way I am getting jobData instance is either

 return context.Jobs.Find(id);

or

return context.Jobs.SingleOrDefault(job => job.JobId == id);
1
1
8/14/2017 11:01:11 AM

Popular Answer

FrameData, Card, Tags, DriverInfo are all one-to-one entites with TaskInfo:

You don't need to reference for example TaskInfo as having a DriverInfo object and the DriverInfo object with the TaskInfo object. Just have one object referencing the other object.

TaskInfo has FKs which are PKs of the other objects FrameData, Card, Tags, DriverInfo.

public class TaskInfo
{
    public int TaskInfoId { get; set; }
    public int ExecId { get; set; }
    .../...
    public int DriverInfoId  { get; set; } // The PK of DriverInfo
    public virtual DriverInfo DriverInfo { get; set; }

}

Remove reference to TaskInfo.

public class DriverInfo
{       
    public int DriverId { get; set; }
    public int DriverInfoId { get; set; }
}

It would be better to use FirstOrdefault as it will handle a null return.

return context.Jobs.FirstOrDefault(job => job.JobId == id);
0
8/14/2017 10:16:13 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow