A data model represents the objects, processes, and rules of an application’s domain. In ASP.NET Core, it commonly consists of C# domain objects and the methods that manipulate them. The application gives users ways to access and interact with that model. Model classes conventionally live in a Models folder. section 3.2.2
Start with the data the application needs. An RSVP application can use one GuestResponse class with nullable Name, Email, Phone, and WillAttend properties. Those properties give the form and its processing code a shared representation of a response; their nullability also matters when validation is added. section 3.2.2 section 3.0
For database-backed features, define classes for the entities and relationships you need. A product catalog can use Product, Category, and Supplier classes, each with a database-allocated key. Foreign keys and navigation properties connect related records. A DbContext exposes DbSet properties for querying them; Entity Framework Core can use the classes to create the database schema. section 18.2.2 section 18.2
Listing 18.7 The contents of the Product.cs file in the Models folder
using System.ComponentModel.DataAnnotations.Schema;
namespace WebApp.Models {
public class Product {
public long ProductId { get; set; }
public required string Name { get; set; }
[Column(TypeName = "decimal(8, 2)")]
public decimal Price { get; set; }
public long CategoryId { get; set; }
public Category? Category { get; set; }
public long SupplierId { get; set; }
public Supplier? Supplier { get; set; }
}
}
The Product listing shows both sides of a relationship: CategoryId and SupplierId hold foreign keys, while Category and Supplier are navigation properties. Its Column attribute selects decimal(8, 2) for Price because C# and SQL numeric types do not map one-to-one. section 18.2.2
Expect the model to change. To track shipped orders, add a Shipped property to Order, then create an Entity Framework Core migration so the database schema can follow the class. Do not treat database access as a black box: inefficient LINQ queries can retrieve far more data than the application needs, so inspect the SQL they produce. section 10.2.1 section 17.5






















