Author.cs
Book.cs to create the model file 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; }
}
}
|
Book is discovered as the principal in the relationship, and Author is discovered as the dependent.
Author.BookId is discovered as a foreign key of the dependent referencing the Book.Id primary key of the principal.
The relationship is discovered as required because Author.BookId is not nullable.
Book.Authors is discovered as the collection navigation.
Author.Book is discovered as the reference navigation.
Book.Id.
Author.BookId.
Book.Authors.
Author.Book.
Author.BookId is not nullable.
This makes the relationship “required” because every dependent (Author) must be related to some principal (Book), since its foreign key property must be set to some value.
using System.ComponentModel.DataAnnotations;|
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. |