Fluent API - one to many

后端 未结 1 1848
旧时难觅i
旧时难觅i 2021-01-02 00:23

I cannot seem to find a one-to-many relationship syntax in the fluent API.

As an example I have two tables as below

User

Id
         


        
1条回答
  •  自闭症患者
    2021-01-02 01:10

    In your model a User entity has many Histories with each history having a required User and the relationship has a foreign key called UserId:

    modelBuilder.Entity()
       .HasMany(u => u.Histories)
       .WithRequired()
       .HasForeignKey(h => h.UserId);
    

    (The relationship must be required (not optional) because the foreign key property UserId is not nullable.)

    0 讨论(0)
提交回复
热议问题