text
stringlengths
0
13.4k
The Id property is a guid, or a globally unique identifier. Guids (or
GUIDs) are long strings of letters and numbers, like 43ec09f2-7f70-
4f4b-9559-65011d5781bb . Because guids are random and are
extremely unlikely to be accidentally duplicated, they are commonly
used as unique IDs. You could also use a number (integer) as a
database entity ID, but you'd need to configure your database to
always increment the number when new rows are added to the
database. Guids are generated randomly, so you don't have to worry
about auto-incrementing.
The IsDone property is a boolean (true/false value). By default, it
will be false for all new items. Later you'll use write code to switch
this property to true when the user clicks an item's checkbox in
the view.
The Title property is a string (text value). This will hold the name or
description of the to-do item. The [Required] attribute tells
ASP.NET Core that this string can't be null or empty.
The DueAt property is a DateTimeOffset , which is a C# type that
stores a date/time stamp along with a timezone offset from UTC.
Storing the date, time, and timezone offset together makes it easy to
render dates accurately on systems in different timezones.
Notice the ? question mark after the DateTimeOffset type? That marks
the DueAt property as nullable, or optional. If the ? wasn't included,
every to-do item would need to have a due date. The Id and IsDone
properties aren't marked as nullable, so they are required and will always
have a value (or a default value).
Strings in C# are always nullable, so there's no need to mark the
Title property as nullable. C# strings can be null, empty, or contain
text.
25
Create models
Each property is followed by get; set; , which is a shorthand way of
saying the property is read/write (or, more technically, it has a getter and
setter methods).
At this point, it doesn't matter what the underlying database technology
is. It could be SQL Server, MySQL, MongoDB, Redis, or something more
exotic. This model defines what the database row or entry will look like
in C# so you don't have to worry about the low-level database stuff in
your code. This simple style of model is sometimes called a "plain old C#
object" or POCO.
The view model
Often, the model (entity) you store in the database is similar but not
exactly the same as the model you want to use in MVC (the view model).
In this case, the TodoItem model represents a single item in the
database, but the view might need to display two, ten, or a hundred to-
do items (depending on how badly the user is procrastinating).
Because of this, the view model should be a separate class that holds an
array of TodoItem s:
Models/TodoViewModel.cs
namespace AspNetCoreTodo.Models
{