2012-03-20 13 views
0

私はまだモデルファーストからコードファーストのEntityFrameworkへの移植を検討しています。私はErangaの助けを借りて、大きな進歩を遂げました。私は別の悩みにぶつかってしまいました。何が起こっているのか説明できません。私は、私は、次のLINQを実行するとき、それは奇妙なSQLを生成し、エンティティがトピックとコースがEntityframework最初のコードからモデル番号への最初のコード

  • Aトピックはコースが0または複数のトピック

を持つことができます

  • 必要とされる1コースを持つことができるオブジェクト2を持っています

    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は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

    関連する問題