简体   繁体   中英

Repository And Unit Of Work : Entity no loading based on foreign key value

I am using Repository and Unit of Work Pattern and my classes are as follows

CourseInstructor Entity

[Serializable]
public class CourseInstructor : AuditableEntity
{        
    #region Primitive Properties
    [Required(ErrorMessage = "CourseID is required.")]
    [ForeignKey("Course")]
    public int CourseID { get; set; }

    [Required(ErrorMessage = "StudentID is required.")]
    [ForeignKey("Student")]
    public int PersonID { get; set; }
    #endregion

    #region Navigation Properties

    public Course Course { get; set; }
    public Person Student { get; set; }
    #endregion
}

Person Entity

[Serializable]
public class Person : AuditableEntity
{
    #region Primitive Properties
    [Required(ErrorMessage = "LastName is required."), StringLength(50, ErrorMessage = "LastName is too long!")]
    public string LastName{ get; set; }


    [Required(ErrorMessage = "FirstName is required."), StringLength(50, ErrorMessage = "FirstName is too long!")]
    public string FirstName{ get; set; }

    [Required(ErrorMessage = "HireDate is required.")]
    public DateTime? HireDate{ get; set; }

    [Required(ErrorMessage = "EnrollmentDate is required.")]
    public DateTime? EnrollmentDate{ get; set; }

    #endregion

    #region Navigation Properties

    public virtual ICollection<CourseInstructor> CourseInstructor{ get; set; }
    //Have other navigation properties to some other entities

    #endregion
}

Course Entity

[Serializable]
public class Course : AuditableEntity
{
    #region Primitive Properties
    [Required(ErrorMessage = "Title is required."), StringLength(100, ErrorMessage = "Title is too long!")]
    public string Title
    {get;set;}

    [Required(ErrorMessage = "Credits is required.")]
    public int Credits
    {get;set;}

    [Required(ErrorMessage = "DepartmentID is required.")]
    public int DepartmentID
    {get;set;}
    #endregion

    #region Navigation Properties
    public Department Department { get; set; }


    public virtual ICollection<OnlineCourse> OnlineCourses
    {get;set;}
    public virtual ICollection<OnsiteCourse> OnsiteCourses
    {get;set;}
    public virtual ICollection<StudentGrade> CourseStudentGrades
    {get;set;}
    public virtual ICollection<CourseInstructor> CourseInstructor
    {get;set;}
    #endregion
}

CourseInstructorConfig

public class CourseInstructorConfig : MyProjectConfig<CourseInstructor>
{
    public CourseInstructorConfig()
    {       
        base.HasRequired<Course>((CourseInstructor a) => a.Course)
           .WithMany((Course c) => c.CourseInstructor)
           .HasForeignKey<int>((CourseInstructor d) => d.CourseID);

        base.HasRequired<Person>((CourseInstructor w) => w.Student)
          .WithMany((Person p) => p.CourseInstructor)
          .HasForeignKey<int>((CourseInstructor q) => q.PersonID);
    }
}

CourseConfig

public class CourseConfig : MyProjectConfig<Course>
{
    public CourseConfig()
    {
        base.HasRequired<Department>((Course c) => c.Department)
            .WithMany((Department d) => d.Courses)
            .HasForeignKey<int>((Course cs) => cs.DepartmentID);            
    }
}

CourseInstructorManager

public class CourseInstructorManager
{
    public static List<CourseInstructor> GetCourseInstructor()
    {
        return new CourseInstructorRepository().GetAll().ToList();
    }
}

When I am trying to get the data of CourseInstructor, I am getting value of Course as null also value of Student as null. Can anybody tell me what is the problem in above code? Or should I do any corrections or additions? How can I brong those entitites filles

List<CourseInstructor> c = CourseInstructorManager.GetCourseInstructor();
Course co = c[0].Course;
Person p = c[0].Student;    

在此处输入图片说明

co and p both are null.

You should use virtual keyword, when you want to load data with lazy loading. So your CourseInstructor shoul be

public class CourseInstructor : AuditableEntity
{        

   #region Primitive Properties
   [Required(ErrorMessage = "CourseID is required.")]
   [ForeignKey("Course")]
   public int CourseID { get; set; }

   [Required(ErrorMessage = "StudentID is required.")]
   [ForeignKey("Student")]
   public int PersonID { get; set; }
   #endregion

   #region Navigation Properties

   public virtual Course Course { get; set; }
   public virtual Person Student { get; set; }
   #endregion 
}

Useful article about this: Requirements for Creating POCO Proxies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM