0

I'm transitioning from nHibernate to EF5 and am having problems with mapping relations. Working with a conventional one-to-many relation let's call it Instructor/Course:

public class Course
{
    public Course()
    {
        Instructor = new Instructor();
        CourseFiles = new List<CourseFile>();
    }

    public int Id { get; set; }
    public string Description { get; set; }
    public string Title { get; set; }
    public int InstructorId { get; set; }

    public virtual Instructor Instructor { get; set; }
    public virtual ICollection<CourseFile> CourseFiles { get; set; }
}

public class CourseMap : EntityTypeConfiguration<Course>
{
    public CourseMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Description)
            .IsRequired();

        this.Property(t => t.Title)
            .IsRequired()
            .HasMaxLength(125);


        // Table & Column Mappings
        this.ToTable("Course");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.Description).HasColumnName("Description");
        this.Property(t => t.Title).HasColumnName("Title");
        this.Property(t => t.InstructorId).HasColumnName("InstructorId");

        // Relationships

        this.HasRequired(t => t.Instructor)
            .WithMany(t => t.Courses)
            .HasForeignKey(d => d.InstructorId);

    }
}

public partial class Instructor
{
    public Instructor()
    {
        this.Courses = new List<Course>();
    }

    public int Id { get; set; }
    public string Biography { get; set; }

    public virtual ICollection<Course> Courses { get; set; }
}

public class InstructorMap : EntityTypeConfiguration<Instructor>
{
    public InstructorMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        this.Property(t => t.Biography)
            .HasMaxLength(140);

        // Table & Column Mappings
        this.ToTable("Instructor");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.Biography).HasColumnName("Biography");

        //the mapping of this relation _has to be where the problem is 
        // really seems like this should create the required plumbing but no joy
        this.HasMany(w => w.Webinars)
            .WithRequired()
            .HasForeignKey(w => w.PresenterId);

    }
}

}

The above is the POCO generated by the Reverse Engineer tool where I've used as a starting point, the db that nHibernate's been using for the last few years. For the most part things lined up really well. After minor adjustments for PKey names I've been able to seed the new db with nHibernate's data and the same joins are working against both dbs.

E.G. this works the same returns correct values:

SELECT C.Title, C.Id, C.InstructorId, I.Id 
    FROM dbo.Course C 
    INNER JOIN dbo.Instructor I ON I.Id = C.InstructorId
    ...

And even within the assembly, this linq query pulls correctly against the db:

        var query = from Course in _ctx.Courses
                    where Course.Instructor.Id == InstructorId
        select Course;

So I must have lots of the ducks lined up correctly. However, when attempting to access the Instructor entity from _within the Course entity, as is typical at the View:

@foreach (var course in Model)
{
    <div>@course.Title - Instructor.ID is all zeros: @course.Instructor.Id - FK is correct: @course.InstructorId</div>
}

outputs:

My Correct Course Title - Instructor.ID is all zeros: 0 - FK is correct: 555
Title From Diff Instructor  - Instructor.ID is all zeros: 0 - FK is correct: 333

and so on for all courses.

I'm not taking explicit action to Dispose anything. I set a breakpoint at the start of the ForEach inside the view and don't see 'context' in the Locals window. Breaking inside the Controller the Instructor node (of context) shows a line for each and every presenter in the db all properly seeded except the first 10 (the Repository is using '.Take(10)'. Those first 10 Presenter lines (in Presenter|Local) are all zeroed out. So clearly, something's up with the Constructor in the POCO.

justSteve
  • 5,444
  • 19
  • 72
  • 137
  • Offtopic - Why didn't you use existing VS template for generating DbContext from an existing database, if you already have one? You'd get a visual editor for entity model and POCOs for data classes, and entity model would be tightly-bound to DB schema reducing potential errors later. I found that preferable to code-first/fluent approach if I had an existing db to use. – Boris B. Aug 18 '13 at 20:23
  • That's probably a valid suggestion and given the stubbornness of this current problem I should probably give the designer a shot. My initial thought was that using CF would provide a deeper learning experience - which it most definitely is. – justSteve Aug 18 '13 at 23:45

0 Answers0