Entity Framework 4, inserting with foreign key value not ID
Asked Answered
T

3

5

This is pretty basic question I am sure but it's baffling me now :S

I have 2 tables, Students and Courses with a foreign key constraint, Many Students to 1 Course.

I store in my Students table a CourseId_FK reference, so when I am creating a Student how would I be able to do something like this?

Students student = new Students();
student.Name = Bob;
student.Course.CourseName = "Geography";
db.SaveChanges();

Right now, the above doesn't work and I have to resolve to

student.CourseId = 22;

Can anyone help me pls?

Thanks

David

Tybalt answered 24/5, 2011 at 16:20 Comment(2)
I'm a little confused, because student=> course is 1 to many, so should it be student.Courses?Sclerotomy
@Vimvq1987 yes I am sorry, I made a mistake in my initial writing of my post, I have since corrected it, Many students to 1 course.Tybalt
A
6

If you don't want to load the full course entity:

Student student = new Student();
student.Name = Bob;
student.CourseId = db.Courses
                     .Where(c => c.CourseName == "Geography")
                     .Select(c => c.CourseId).First();
db.SaveChanges();

This would only fetch the Id of the Geography course from DB (or crash when there isn't any).

Adriaadriaens answered 24/5, 2011 at 16:41 Comment(1)
Thank you, this was what I was looking. The other answers opened my eyes though as well. Thank you allTybalt
N
5

Looking at your entities, does "Student" have a "Course" (Really: Courses) property?

Your entity should probably look something like this, for what you want to do:

public class Student
{
    public string Name {get; set;}

    public virtual ICollection<Course> Courses {get; set;}
}

Then you'd do something like:

Student student = new Student();
student.Name = Bob;
student.Courses.Add(db.Courses.Where(c => c.CourseName == "Geography"));

Edit: If the Student can only have one course, you modify the Student class so it looks like this:

public class Student
{
    public int StudentId {get; set;}
    public string Name {get; set;}
    public int CourseId {get; set;}
    public virtual Course Course {get; set;}
}

Then your code looks like this:

student.Course = db.Courses.Where(c => c.CourseName == "Geography"));

What's going on with your code (I expect) is that you only have the CourseId so when you're so you can't assign an actual course object to your student.

Negligible answered 24/5, 2011 at 16:31 Comment(2)
Yes I apologies, 1 student can have only 1 course, I made a mistake when I was writing the original question.Tybalt
Thank you, this one opened my eyes to make me understand more about Entity Framework, thank youTybalt
C
2

I'd do something like this:

Student student = new Student();
student.Name = Bob;
student.Course = db.Courses.First(c => c.CourseName == "Geography");
db.SaveChanges();

Update: the answer is about the situation when a student has zero or one course (as in the provided code).

Chaffin answered 24/5, 2011 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.