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.
(Continued from previous post above.)
Once again, Entity1 and Entity2 are 1:1. Entity1 refers to Entity2 with nav-prop "ToEntity2"; Entity2 refers to Entity1 with "ToEntity1". I need to know how to get (existing) or create (new) an instance of Entity2, such that it's linked to an instance of Entity1. What's right/wrong about the sample code below?
' add new instance of 2nd entity to new 1st-entity instance
Dim NewE1 As Entity1 = some value
Dim NewE2 As Entity2 = some value
NewE1.ToEntity2 = NewE2
db.Set(Of Entity1)().Add(NewE1) : db.SaveChanges()
' load existing instance of 2nd entity from existing 1st-entity instance
Dim OldE1 As Entity1 = result from query
db.Entry(OldE1).Reference("ToEntity2").Load()
Dim OldE2 As Entity2 = OldE1.ToEntity2
' change instance of 2nd entity in existing 1st-entity instance
Dim NewE2 As Entity2 = some value
OldE1.ToEntity2 = NewE2 : db.SaveChanges()
Also, what about when I have to delete an instance of Entity1--in which case, I'll want the corresponding Entity2-instance to be removed as well? Would the code be like this, in which removing one automatically removes the other?
Dim OldE1 As Entity1 = result from query
db.Set(Of Entity1)().Remove(OldE1) : db.SaveChanges()
Or like this, in which they have to be removed separately?
Dim OldE1 As Entity1 = result from query
db,Entry(OldE1).Reference("ToEntity2").Load()
Dim OldE2 As Entity2 = OldE1.ToEntity2
db.Set(Of Entity2)().Remove(OldE2) : OldE1.ToEntity2 = Nothing
db.Set(Of Entity1)().Remove(OldE1) : db.SaveChanges()
Hi @Robert Gustafson ,
I have updated my answer, you can refer to it.
Best Regards,
Daniel Zhang
So the sample code I provided for adding a new instance of Entity1 with an instance of Entity2, or for changing an instance of Entity2 for an existing instance of Entity1, should work properly? And in the case of removing an instance of Entity1, I should follow the second coding example I provided of that, in which the Entity2 instance is removed first? If there's anything I should do differently, please say so. (You might want to provide a psuedocode code sample similar to what I provided. A full example isn't necessary; just provide enough info to serve as a template.) Get back to me ASAP because I want to act (or not act) quickly. All in all, I need to know what the host code may have to do differently at run-time when it comes to 1:1 relationships as opposed to 1:many or many:many. (I guess my biggest uncertainty is whether or not deleting the primary entity in a 1:1 relationship is a cascading delete or not.)
Hi @Robert Gustafson ,
In your example, Entity1 is the principal/parent, and Entity2 is the dependent/child. If Entity1 is deleted, Entity2 need to be deleted at the same time. This will not cause a reference constraint conflict. And this is cascading delete.
And if you just want to delete dependent/child entity, you can set the reference navigation to null to sever the relationship, and then delete Entity2. Deleting entities that are no longer associated with any principal/dependent is known as "deleting orphans".
Best Regards,
Daniel Zhang
Sign in to comment