liveBook · Manning

Data model

As explained in Pro ASP.NET Core 7, Tenth Edition

A data model is a structured representation of the real-world objects, processes, rules, and data that constitute an application's domain.

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

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Works from a single class through related entities, context properties, and schema migrations; leaves detailed ORM query behavior to further investigation.
Examples
Uses an RSVP response, a product catalog, shipped orders, and calculations to illustrate models of increasing scope.
What it assumes
Assumes familiarity with C# classes and ASP.NET Core projects; introduces the model from a simple class and expects SQL knowledge for efficient queries.
What you can do after
Build it from scratch with C# model classes and an Entity Framework Core context, then evolve database-backed models with migrations.
How it connects
Connects domain objects to form validation, Entity Framework Core, database schemas, foreign keys, navigation properties, and migrations.

Also explained in

Compare how 23 books explain data model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.