Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:37:20+00:00 2026-05-31T18:37:20+00:00

I am still on my quest to port from a Model First to Code

  • 0

I am still on my quest to port from a Model First to Code First implementation of EntityFramework. I have made significant progress, with the help of Eranga. I have run into another snag, and I just cant explain what is hapening. I have two Entity objects Topic and Course

  • A Topic can have one Course that is required
  • A Course can have 0 or more topics

when i execute the following linq it generates wierd SQL

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

The SQL generated is

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)

Notice that there is an added field called Course_Id1 that is not in my object and not declared as a foreign key. I thought that in OnModelCreating() I had specified the parent child relationship correctly from both sides (I would have thought you only needed to do it from either side), but i cant get EntityFramework not to generate the extra field that obviously does not exist in the database. Remember my database was originally created using a ModelFirst approach.

Can anyone explain where the extra field is comming from????

    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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:37:22+00:00Added an answer on May 31, 2026 at 6:37 pm

    You have another relationship between Course and Topic created by convention due to this navigation property:

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

    EF will put an (invisible, not exposed) end of the relationship into the Topic class. By default the relationship is one-to-many. Hence you get an additional foreign key property in the Topics table (= Course_Id1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have made quite a bit of progress in my quest to figuring out the
I have a requirement where users should be able to navigate from the first
still new to the world of linq, and i need some help flatening a
still new to XML parsing with the iphone so i have a few questions.
I'm still on my quest for a really simple language and I know now
EDIT: Looking for help from J. Leffler, Cheese Con Queso or anyone who knows
still pretty new to android I have to clone an iphone application. Im having
Still on the BigNerdRanch iOS Development book. In the Accelerometer chapter, they first implement
I'm still on my eternal quest to build (and understand) modern programming convention of
In my Quest to understanding Mnesia, I still struggle with thinking in relational terms.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.