How to represent Bridge table in Entity Framework Code First

前端 未结 2 1649
刺人心
刺人心 2021-01-04 07:58

I am trying to find out how I can represent a bridge table between two entities (many to many relation) I\'m Using Entity Framework Code First

Student:
   St         


        
2条回答
  •  梦谈多话
    2021-01-04 08:33

    In short: This relationship type between Student and Class is called many to many relationship in EF terminology.

    Entity framework handles tables participating in Many to Many relationship in a nice manner. If the junction table (sometimes called bridge table, association table, link table, etc) only consists of the foreign keys and no other columns, then that table is abstracted by EF and the two sides get a navigational property exposing a collection of the other side.

    What you would need is to generate a Entity Model for the above tables first or define your code first structure like in the following post - Creating a Many To Many Mapping Using Code First.

    Basically, depending on your structure you need to set the method OnModelCreating appropriate to the relationships in your classes.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    

    Depending on your query needs you will find it informative to look at the following references:

    • Queries involving many to many relationship tables
    • Many To Many Mappings in Entity Framework
    • Many-to-Many Relationships with Entity Framework

提交回复
热议问题