MVC Models: Author.cs


Apply the same steps for creating Book.cs to create the model file Author.cs:


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: A one-to-many relationship is made up from: So, for the relationship in this example:
using System.ComponentModel.DataAnnotations;
The namespace provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls.




      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.