Are there any special considerations for *1-to-1* relationships in regards to Entity Framework?

Robert Gustafson 606 Reputation points
2021-06-28T04:01:52.043+00:00

WHAT I HAVE:
Visual Basic 2019, Entity Framework 6+

MY PROBLEM:
Coding for 1-to-1 Entity Relationships

I have a fairly good understanding of how to write code for EF entities with 1-to-many, many-to-1, and many-to-many relationships (not enough documentation in books on the latter; I had to do a lot of trial and error on many-to-many code logic!);--however, I don't know very much about how to code for entities which are related 1-to-1 (or "0-or-1" to 1, which is also supported). I imagine that 1-to-1 relationships--say, 1 "PersonDescription" entity for every "Person" entity, and vice versa--might be useful when certain properties (like a large picture or a rich-text-document string) in a primary entity type (say, "Person") use a lot of memory, and therefore should be placed instead in a secondary entity type (say, "PersonDescription"), whose instance is loaded only when needed. Whatever the reason, I need to know what the run-time conventions are for using members which, say, add/modify/remove/load a secondary entity which is related to a primary one on a 1-to-1 basis. Are the rules for using methods like Add, Remove, and Load any different in 1-to-1 scenarios? Do such entities behave in any special ways (other than "by-definition" differences)? (BTW, my apps are generally WinForms and use Model-First EFs. But that's not important; I need to know how to handle 1-to-1 entities at run-time.)

Please answer ASAP, with any code examples in VB.NET.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,356 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-06-28T07:10:58.283+00:00

    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:

    1. 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.
    2. 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.


0 additional answers

Sort by: Most helpful