Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show correct code sample for optional one-to-many #4357

Merged
merged 1 commit into from
May 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions entity-framework/core/modeling/relationships/one-to-many.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,18 @@ Neither of these options is better than the other; they both result in exactly t
public class Blog
{
public int Id { get; set; }
public ICollection<Post> Posts { get; } = new List<Post>(); // Collection navigation containing dependents
public ICollection<Post> Posts { get; } = new List<Post>(); // Collection navigation containing dependents
}

// Dependent (child)
public class Post
{
public int Id { get; set; }
public Blog? Blog { get; set; } // Optional reference navigation to principal
public int? BlogId { get; set; } // Optional foreign key property
public Blog? Blog { get; set; } // Optional reference navigation to principal
}
-->
[!code-csharp[OneToManyOptionalShadow](../../../../samples/core/Modeling/Relationships/OneToMany.cs?name=OneToManyOptionalShadow)]
[!code-csharp[OneToManyOptional](../../../../samples/core/Modeling/Relationships/OneToMany.cs?name=OneToManyOptional)]

This is the same as the previous example, except that the foreign key property and navigation to the principal are now nullable. This makes the relationship "optional" because a dependent (`Post`) can exist _without_ being related to any principal (`Blog`).

Expand Down