Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core One to One or Zero Relationship

Tags:

I have Person and Address. Address is optional. Please see below code

class Person {     [Key]     public int PersonID { get; set; }     public string Name { get; set; }      public Address Address { get; set; }  }  class Address {     [Key, ForeignKey("Person")]     public int PersonID { get; set; }      public string City { get; set; } } 

Registration code is below:

modelBuilder.Entity<Address>(entity =>             {                 entity.HasKey(z => z.PersonID);                 entity.HasOne(p => p.Person)                      .WithOne(a => a.Address)                      .HasForeignKey<Person>(a => a.PersonId);             }); 

How should i change mapping to make Address optionable?

like image 446
Siarhei Kuchuk Avatar asked Mar 04 '19 14:03

Siarhei Kuchuk


People also ask

Is EF core a relationship?

In Entity Framework Core, relationship means how two or more entities related to each other in the database. In a database, there can be multiple entities that relate to each other in terms of their entity classes or entity types.

Is lazy loading good EF core?

Lazy loading of data is a pattern whereby the retrieval of data from the database is deferred until it is actually needed. This sounds like a good thing, and in some scenarios, this can help to improve the performance of an application.

What are the different relationship patterns in Entity Framework?

Entity framework supports three types of relationships, same as database: 1) One-to-One 2) One-to-ManyOne-to-ManyIn systems analysis, a one-to-many relationship is a type of cardinality that refers to the relationship between two entities (see also entity–relationship model) A and B in which an element of A may be linked to many elements of B, but a member of B is linked to only one element of A.https://en.wikipedia.org › wiki › One-to-many_(data_model)One-to-many (data model) - Wikipedia, and 3) Many-to-Many.

What's recommended approach for using EF in disconnected application?

EF Core can only track one instance of any entity with a given primary key value. The best way to avoid this being an issue is to use a short-lived context for each unit-of-work such that the context starts empty, has entities attached to it, saves those entities, and then the context is disposed and discarded.


1 Answers

Here

.HasForeignKey<Person>(a => a.PersonId) 

you are telling EF that Person.PersonId will be a FK (foreign key) to Address, i.e. Person is dependent and is referencing the principal Address.

It should be other way around:

.HasForeignKey<Address>(a => a.PersonId) 

This way, Person (the principal) will have 0..1 Address, and Address (the dependent) will have 1 Person (because the PersonId is both PK and FK).

This is called Shared Primary Key association and is the standard (and default) way of modelling one to zero or one relationship in EF Core.

For more info, see Relationships.

like image 103
Ivan Stoev Avatar answered Sep 21 '22 18:09

Ivan Stoev