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
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.)