0

我正在从 nHibernate 过渡到 EF5,并且在映射关系方面遇到问题。使用传统的一对多关系,我们称之为讲师/课程:

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

    }
}

}

以上是我作为起点使用的逆向工程师工具生成的 POCO,nHibernate 过去几年一直在使用的数据库。在大多数情况下,事情都安排得很好。在对 PKey 名称进行细微调整后,我已经能够使用 nHibernate 的数据为新数据库播种,并且相同的连接对两个数据库都有效。

EG 这同样会返回正确的值:

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

即使在程序集中,这个 linq 查询也能正确地拉动数据库:

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

所以我必须让很多鸭子正确排列。但是,当尝试从 _within Course 实体访问 Instructor 实体时,这在 View 中很典型:

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

输出:

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

等等所有课程。

我没有采取明确的行动来处置任何东西。我在视图内的 ForEach 开头设置了一个断点,但在 Locals 窗口中看不到“上下文”。在控制器内部中断,Instructor 节点(上下文)为数据库中的每个演示者显示一行,除了前 10 个(存储库使用'.Take(10)')外,所有演示者都正确播种。前 10 个演示者行(在演示者中|Local) 全部归零。很明显,POCO 中的构造函数出了点问题。

4

0 回答 0