I am confused in two ways to create one to many relationship in code first migration . Which one to choose?

Alamzaib Farooq

I have used two ways to create one to many relationships in code first migration and I do not know what is the difference between each of them.

First method:

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

public class Course
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Student Student { get; set; }
}

Second Method:

 public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Course> Courses { get; set; }
}

public class Course
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Student Student { get; set; }
}

They both have the same result. See the relationship: Relationship Diagram

So why we use ICollection in student table if we get the same relationship?

Ashiquzzaman

A relationship in the entity framework always has two ends, a navigation property on each side and an Entity Framework that maps them together automatically by convention.

In your example, Student and Course class have a one-to-many relationship. Here, Id (Student table) will become the primary key of the Student table and ForeignKey attributes for Student_Id property in Course class in order to make foreign key.You have to pass Student entity in ForeignKey attribute of Course class. Thus, the code first creates a one-to-many relation between Student and Course class using DataAnnotations attributes.

For Example:

You can simple get all course Name of a student by using negation property of student able (Courses) like:

_context.Students
       .Include(s => s.Courses)     
       .Courses.Select(c=>c.Name).ToList();

You can fiend more information about One-to-Many this link.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

One to many recursive relationship with Code First

From Dev

EF code first make one to many relationship

From Dev

How to create nullable one-to-many relationship in Entity Framwork 6 code first

From Dev

One to many relationship with junction table using Entity Framework code first

From Dev

Code First one to many relationship and category with parent category

From Dev

The best practice to design one to many relationship in EF code first

From Dev

One to many relationship with junction table using Entity Framework code first

From Dev

Two way one to many relationship

From Dev

Listener pattern - there are two ways I can implement it, which one?

From Dev

How do I create a one to many relationship in Parse?

From Dev

Code First Optional One-To-One Relationship

From Dev

JMS Serializer. Create 2 ways of model's serialization that has "one-to-many" relationship

From Dev

EF Code First One To Many

From Dev

Entity Framework (Code First) One to Many and One to One relations (with two entities). How to?

From Dev

Have two entity in Grails with one to many relationship

From Dev

Query two tables with one to many relationship

From Dev

How to create an object in a one to many relationship

From Dev

create mysql table with one to many relationship

From Dev

How to create an object in a one to many relationship

From Dev

Alembic migration issue with PostgreSQL schema and invalid one-to-many relationship

From Dev

How to create one-to-one relationship between same table in Entity Framework 6 Code First?

From Dev

how to define many-to-many and one-to-many relations between two entities in code first?

From Dev

Entity Framework, Code First and One-to-Many relationship across multiple contexts

From Dev

Entity framework 6 Code First one-to-many optional relationship remove child issue

From Dev

One to many relationship with code first. Where this foreign-key came from?

From Dev

EF Code First One-to-One AND One-to-Many mapping

From Dev

how many ways to check if x is integer and which one is most efficient?

From Dev

PHPUnit_Extensions_Selenium2TestCase vs PHPUnit_Extensions_SeleniumTestCase ?? i am very confused as to which one to use

From Dev

PHPUnit_Extensions_Selenium2TestCase vs PHPUnit_Extensions_SeleniumTestCase ?? i am very confused as to which one to use

Related Related

  1. 1

    One to many recursive relationship with Code First

  2. 2

    EF code first make one to many relationship

  3. 3

    How to create nullable one-to-many relationship in Entity Framwork 6 code first

  4. 4

    One to many relationship with junction table using Entity Framework code first

  5. 5

    Code First one to many relationship and category with parent category

  6. 6

    The best practice to design one to many relationship in EF code first

  7. 7

    One to many relationship with junction table using Entity Framework code first

  8. 8

    Two way one to many relationship

  9. 9

    Listener pattern - there are two ways I can implement it, which one?

  10. 10

    How do I create a one to many relationship in Parse?

  11. 11

    Code First Optional One-To-One Relationship

  12. 12

    JMS Serializer. Create 2 ways of model's serialization that has "one-to-many" relationship

  13. 13

    EF Code First One To Many

  14. 14

    Entity Framework (Code First) One to Many and One to One relations (with two entities). How to?

  15. 15

    Have two entity in Grails with one to many relationship

  16. 16

    Query two tables with one to many relationship

  17. 17

    How to create an object in a one to many relationship

  18. 18

    create mysql table with one to many relationship

  19. 19

    How to create an object in a one to many relationship

  20. 20

    Alembic migration issue with PostgreSQL schema and invalid one-to-many relationship

  21. 21

    How to create one-to-one relationship between same table in Entity Framework 6 Code First?

  22. 22

    how to define many-to-many and one-to-many relations between two entities in code first?

  23. 23

    Entity Framework, Code First and One-to-Many relationship across multiple contexts

  24. 24

    Entity framework 6 Code First one-to-many optional relationship remove child issue

  25. 25

    One to many relationship with code first. Where this foreign-key came from?

  26. 26

    EF Code First One-to-One AND One-to-Many mapping

  27. 27

    how many ways to check if x is integer and which one is most efficient?

  28. 28

    PHPUnit_Extensions_Selenium2TestCase vs PHPUnit_Extensions_SeleniumTestCase ?? i am very confused as to which one to use

  29. 29

    PHPUnit_Extensions_Selenium2TestCase vs PHPUnit_Extensions_SeleniumTestCase ?? i am very confused as to which one to use

HotTag

Archive