Hi RobertGustafson-1682,
One to one relationships have a reference navigation property on both sides. They follow the same conventions as one-to-many relationships, but a unique index is introduced on the foreign key property to ensure only one dependent is related to each principal.
And you can use configure this relationship with the Fluent API:
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As ModelBuilder)
modelBuilder.Entity(Of Blog)().HasOne(Function(b) b.BlogImage).WithOne(Function(i) i.Blog).HasForeignKey(Of BlogImage)(Function(b) b.BlogForeignKey)
End Sub
For more details, you can refer to the One-to-one section of this document.
Update:
You need to pay attention to the following aspects:
- Adding a related entity
First, you can use DbContext.Entry which returns an EntityEntry<TEntity> instance for a given entity instance.
If you reference a new entity from the navigation property of an entity that is already tracked by the context, the entity will be discovered and inserted into the database. - Removing relationships:
You can remove a relationship by setting a reference navigation to null, or removing the related entity from a collection navigation.
If the principal/parent entity is deleted, then the foreign key values of the dependents/children will no longer match the primary or alternate key of any principal/parent. This is an invalid state, and will cause a referential constraint violation in most databases.
More details please refer to the following documents:
Accessing Tracked Entities
Saving Related Data
Cascade Delete
Best Regards,
Daniel Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.