Apply the same steps for creating to create the model file :
C:\ASP.NET-workspace\BookStore3\Models\Author.cs
using System.ComponentModel.DataAnnotations;
namespace BookStore3.Models {
// Dependent (child)
public class Author {
[ Key ]
[ Required ]
public int Id { get; set; }
[ Display( Name = "Author Name" ) ]
[ Required ]
public string Name { get; set; }
// Foreign key property
public int BookId { get; set; }
// Reference navigation property
public Book? Book { get; set; }
}
}
This one-to-many relationship is discovered by convention:
is discovered as the principal in the relationship, and is discovered as the dependent.
is discovered as a foreign key of the dependent referencing the primary key of the principal.
The relationship is discovered as required because is not nullable.
is discovered as the collection navigation.
is discovered as the reference navigation.
A one-to-many relationship is made up from:
One or more primary key properties on the principal entity; that is the “one” end of the relationship; e.g., .
One or more foreign key properties on the dependent entity; that is the “many” end of the relationship; e.g., .
Optionally, a collection navigation on the principal entity referencing the dependent entities; e.g., .
Optionally, a reference navigation on the dependent entity referencing the principal entity; e.g., .
So, for the relationship in this example:
The foreign key property is not nullable.
This makes the relationship “required” because every dependent () must be related to some principal (), since its foreign key property must be set to some value.
Both entities have navigations pointing to the related entity or entities on the other side of the relationship.
President Lincoln was approached by a woman after a political speech…
If you were my husband I would poison your tea.
Lincoln replied...if you are my wife I’ll gladly drink it.