2012-03-20 203 views
0

我仍然在追求從模型優先到代碼優先實現EntityFramework。在Eranga的幫助下,我取得了重大進展。我遇到了另一個障礙,我只是不能解釋什麼是hapening。我有兩個實體對象主題和課程實體框架端口從模型優先到代碼優先

  • 的話題可以有需要一個場
  • 教程可以有0個或多個主題

當我執行以下LINQ它產生奇怪的SQL

var topics = from o in db.Topics where o.ParentTopic == null && 
      o.Course.Id == c.Id select o; 

的SQL生成是

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[Name] AS [Name], 
[Extent1].[ShortDescription] AS [ShortDescription], 
[Extent1].[LongDescription] AS [LongDescription], 
[Extent1].[Property] AS [Property], 
[Extent1].[Difficulty] AS [Difficulty], 
[Extent1].[Weight] AS [Weight], 
[Extent1].[Course_Id] AS [Course_Id], 
[Extent1].[ParentTopic_Id] AS [ParentTopic_Id], 
[Extent1].[Course_Id1] AS [Course_Id1] 
FROM [dbo].[Topics] AS [Extent1] 
WHERE ([Extent1].[ParentTopic_Id] IS NULL) AND ([Extent1].[Course_Id] = @p__linq__0) 

請注意,有一個名爲Course_Id1的添加字段不在我的對象中,也沒有聲明爲外鍵。我認爲,在OnModelCreating()我已經從雙方正確指定父子關係(我會認爲你只需要從任何一方做),但我不能讓EntityFramework不會產生顯然不存在的額外字段在數據庫中。請記住,我的數據庫最初是使用ModelFirst方法創建的。

誰能解釋其中額外字段從????正在添加

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     //Topic 
     modelBuilder.Entity<Topic>() 
      .HasRequired(m => m.Course) 
      .WithMany(m=>m.Topics) 
      .HasForeignKey(m => m.Course_Id); 
     modelBuilder.Entity<Topic>() 
      .HasOptional(m => m.ParentTopic) 
      .WithMany(m => m.ChildTopics) 
      .HasForeignKey(m => m.ParentTopic_Id); 

     //////// lots of code removed for brevity. ////// 

     modelBuilder.Entity<Course>() 
      .HasMany(m=>m.Topics) 
      .WithRequired(m => m.Course) 
      .HasForeignKey(m => m.Course_Id); 
    } 


public partial class Topic 
{ 
    public int Id { get; set; } 
    [Required] 
    public string Name { get; set; } 
    public string ShortDescription { get; set; } 
    public string LongDescription { get; set; } 
    public string Property { get; set; } 
    public double? Difficulty { get; set; } 
    public double? Weight { get; set; } 

    [JsonIgnore] 
    public virtual Course Course { get; set; } 
    public int Course_Id { get; set; } 

    [JsonIgnore] 
    public virtual ICollection<Question> Questions { get; set; } 

    [JsonIgnore] 
    public virtual ICollection<Topic> ChildTopics { get; set; } 

    [JsonIgnore] 
    public virtual Topic ParentTopic { get; set; } 
    public int? ParentTopic_Id { get; set; } 

    [JsonIgnore] 
    public virtual ICollection<RTIQueueEntryData> RTIQueueEntryData { get; set; } 

    [JsonIgnore] 
    public virtual ICollection<Intervention> Interventions { get; set; } 

    [JsonIgnore] 
    public virtual ICollection<RtiStudentGroup> RtiStudentGroups { get; set; } 
} 

public partial class Course 
{ 
    public int Id { get; set; } 
    [Required] 
    public string Name { get; set; } 
    [Required] 
    public string Description { get; set; } 
    public string Version { get; set; } 
    public string Year { get; set; } 
    public string ImportedId { get; set; } 
    [Required] 
    public string LocalCourseNumber { get; set; } 
    [Required] 
    public string NCESCourseNumber { get; set; } 
    [Required] 
    public string StateCourseNumber { get; set; } 
    public int? Grade { get; set; } 

    [JsonIgnore] 
    public virtual ICollection<Topic> PerformanceIndicators { get; set; } 

    [JsonIgnore] 
    public virtual Department Department { get; set; } 
    public int DepartmentId { get; set; } 

    [JsonIgnore] 
    public virtual ICollection<StudentGroup> StudentGroups { get; set; } 

    [JsonIgnore] 
    public virtual ICollection<CutPointTemplate> CutPointTemplates { get; set; } 

    [JsonIgnore] 
    public virtual School School { get; set; } 
    public int School_Id { get; set; } 

    [JsonIgnore] 
    public virtual ICollection<Staff> RTIStaff { get; set; } 

    [JsonIgnore] 
    public virtual ICollection<Topic> Topics { get; set; } 
} 

回答

1

你有CourseTopic按照慣例創建的另一關係由於這種導航屬性:

public virtual ICollection<Topic> PerformanceIndicators { get; set; } 

EF將會把一個(不可見,不暴露)的關係的一端插入Topic類。默認情況下,這種關係是一對多關係。因此,您在Topics表(= Course_Id1)中獲得了額外的外鍵屬性。

+0

你絕對正確我總是應該抓住這一點。非常感謝您的快速週轉。 – Kevin 2012-03-20 18:24:28