Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Ole Vølund Skov Mortensen
Ole Vølund Skov Mortensen
27,841 Points

Why wont EF properly Add the applicant in relation to my question table

Short rundown: The code below resembles a project i am working on. It's supposed to save Applicants names in association with the questions that has been answered into a (currently) implicit join table. The issue with My own code is that when run, produce the following result: applicantID: 1,1,1,1,1,1,1,1,1,1,1,1... QuestionID: 11,12,13,14,15.....

WHERE Do i mess up?!

true it makes a association to the applicant, however instead of associating it to the first question as 1, 2, 3 (there is 10 questions) it duplicates the first question by Adding to the question table as 11,12 and so on. please, Help with my relationship issue.

Program.cs

 var collect = Repository.GetAQuestions();
            Console.Write("Please provide us with a name:");
            var applicant = new Applicant()
            {
                Name = Console.ReadLine()
            };

            foreach (QuestionApplicant item in collect)
            {
                Console.WriteLine(item.Id + " " + item.Description);
                string answer = Console.ReadLine();
                Console.WriteLine(item.Description + " " + answer);
                applicant.Questions.Add(item);
             }
            Repository.AddAQuestion(applicant);

            Console.ReadLine();

Repository.cs

 public static IList<QuestionApplicant> GetAQuestions() {
            using (Context context = GetContext())
            {
                return context.Questions.
                    OrderBy(q => q.Id).ToList();

            }
        }

  public static void AddAQuestion (Applicant question) {
            using (Context context = GetContext())
            {
                context.Applicants.Add(question);
                context.SaveChanges();
            }
        }