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 8611909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:29:35+00:00 2026-06-12T04:29:35+00:00

I have problem because when I add the following to class Course I have

  • 0

I have problem because when I add the following to class Course I have only 2 tables not 3

public int PersonId { get; set; }
[ForeignKey("PersonId")]
public virtual Person Student { get; set; }

you do not have these three lines all good, but I need an additional field in class Course

public class Person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual ICollection<Course> CoursesAttending { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string Title { get; set; }
    public int PersonId { get; set; }

        [ForeignKey("PersonId")]
    public virtual Person Student { get; set; }

    public virtual ICollection<Person> Students { get; set; }
}

public class SchoolContext : DbContext
{
    public DbSet<Course> Courses { get; set; }
    public DbSet<Person> People { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Database.SetInitializer<SchoolContext>(
                new DropCreateDatabaseAlways<SchoolContext>());

        SchoolContext db = new SchoolContext();
        var cos = from d in db.Courses
                  select d;
    }
}

please help me

  • 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-06-12T04:29:37+00:00Added an answer on June 12, 2026 at 4:29 am

    EF cannot decide if Course.Student or Course.Students refers to Person.CoursesAttending. You must give EF a hint, for example by using the [InverseProperty] attribute on Course.Students:

    [InverseProperty("CoursesAttending")]
    public virtual ICollection<Person> Students { get; set; }
    

    Edit

    The model will cause multiple cascading delete paths, namely: When you delete a Person records in the join table will be deleted as well, but it will also delete all Courses that this person is assigned to through the Course.Person property. The deleted Courses will delete records in the join table again which is the second delete path on the same table.

    Multiple cascading delete paths are not allowed with SQL Server, so you must disable it with Fluent API:

    public class MyContext : DbContext
    {
        //...
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Course>()
                .HasRequired(c => c.Student)
                .WithMany()
                .HasForeignKey(c => c.PersonId)
                .WillCascadeOnDelete(false);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following class structure (simplified from my real-world problem): Public
I have the following DB helper class: public int studentExists(String studid) { Cursor dataCount
Say I have the following POCO classes: public class Parent { public int ID
I have got very big problem because I would like to get more information
I have a problem with one-to-many relationships. I have the following domain classes: public
I heave roughly speaking the following hierarchy class Base { public: virtual int value()
My problem is the following, I have a class, let's call it Theatre .
I have a problem trying to get the count out of the following query:
I have the following web service: [ScriptService] public class Handler : WebService { [WebMethod]
The following code is the simplified version of my problem: public partial class Form1

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.