0

I'd like to define relationship where Student can have only one favorite Course. I expect it would look like this in DB:

STUDENT
    ID    
    Name
    FavoriteCourseID
COURSE
    ID
    Name

How to achieve this with entity framework? I'd prefer to specify it just by attributes. I tried:

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

    public Course FavoriteCourse { get; set; }
    public int? FavoriteCourseID { get; set; }
}

public class Class
{
    public int ID { get; set; }
    public string Name { get; set; }
}

which gave me this DB model:

STUDENT
    ID    
    Name
    FavoriteCourseID
COURSE
    ID
    Name
    StudentID // how to remove this?

Note, that it may happen that several students have the same favorite class and therefore this is unacceptable solution.

Another question: what type of relationship this is? (1:1 / 1:N ?)

chriemmy
  • 119
  • 11
  • First - it is not clear why you need and ID if you have a Course. Second - One Student could have only 0 or 1 Class, one Class could be chosen by any amount of Students - so it should be 0..1 to 0..N here – Boris Sokolov Apr 04 '17 at 12:27
  • 1
    It's one to many. And I'm not getting `StudentID` in `Course` table from your model (assuming `Class` is `Course`), so apparently the posted model is incomplete. – Ivan Stoev Apr 04 '17 at 12:30
  • 1
    @IvanStoev Yes, the model I posted is incomplete. There was also many to many relationship between the entities. I assumed the problem was with the definition of `FavoriteCourse` relation. But as it turned out, the problem was, that Entity framework did not [recognize the relation correctly](http://stackoverflow.com/questions/8228948/using-both-many-to-many-and-one-to-many-to-same-entity). – chriemmy Apr 05 '17 at 09:40
  • @chriemmy That's what I thought. So according to your comment I assume you have resolved the issue :) – Ivan Stoev Apr 05 '17 at 10:31
  • 1
    @IvanStoev Yes, I did. Thanks :) – chriemmy Apr 05 '17 at 11:39

3 Answers3

1

To specify 1 to 1 relationship, it is assumed, that primary key for the related entity matches the primary key of first entity. Also you should specify a virtual property to related entity:

public class Student
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }

    public Course FavoriteCourse { get; set; }
    public int? FavoriteCourseID { get; set; }
}

public class Class
{
    [Key]
    [ForeignKey("Student")]
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual Student Student { get; set; }
}

And it will be one-to-zero-or-one relationship. Check this tutorial.

If you will mark FavouriteCourse property with RequiredAttribute, it seems, that it will result in strong one to one relationship.

It will result in adequate database structure:

STUDENT
    ID    
    Name
    FavoriteCourseID
COURSE
    ID
    Name

However, if many students could have one favourite course, this structure will be a problem, as you want one-to-many instead of one-to-one. And you will have a duplicate records in database, because one course can refer only to one student. You have to think about your db design.

Mikhail Tulubaev
  • 4,141
  • 19
  • 31
0

You can try this:

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

    [ForeignKey("FavoriteCourseId")]
    public Course FavoriteCourse { get; set; }
    public int? FavoriteCourseId { get; set; }
}
Mihail Stancescu
  • 4,088
  • 1
  • 16
  • 21
0

Normally, you define one of the following relations:

  1. Optional:Optional
  2. Required:Optional
  3. Optional:Many
  4. Required:Many
  5. Many:Many

Having Required:Required is not a usual relation, inserting the first entry with such a relation needs special treatment.

I Suppose you want Required:Many as in "Each student has one favorite course but many students may chose the same favorite course".

grek40
  • 13,113
  • 1
  • 24
  • 50