Sub topic 3
Defining Entity Classes
An entity class is a plain C# class that EF Core maps to a database table. Each object of the class becomes one row in that table. Each property becomes one column. In this chapter you write the two entity classes the rest of the tutorial uses: Blog and Post.
Why Plain Classes Are Enough
Your entity classes do not need a special base class or any attributes. EF Core uses conventions instead. A convention is a built-in rule that EF Core follows when you have not told it otherwise. For example, one convention says that a property named Id is the primary key. Because of conventions, simple classes need very little setup.
The Blog and Post Classes
Add a file named Model.cs to your project with this code:
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; } = "";
public List<Post> Posts { get; } = new();
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; } = "";
public string Content { get; set; } = "";
public int BlogId { get; set; }
public Blog Blog { get; set; } = null!;
}The code compiles on its own. The DbContext that connects these classes to a database comes in the next chapter.
The Conventions at Work
EF Core reads these classes and applies its rules:
| Code | What EF Core does |
|---|---|
| `BlogId`, `PostId` | Makes them primary keys, because each is named `Id` or `<ClassName>Id` |
| `int` key | Has the database generate the value when you insert a row |
| `string Url` | Creates a text column |
| `int BlogId` in `Post` | Makes it a foreign key to `Blog` |
| `Posts`, `Blog` | Treats them as links between the two classes |
A primary key is the column that uniquely identifies each row. A foreign key is a column that points to a row in another table. Here, Post.BlogId stores the ID of the blog the post belongs to.
Required and Optional Values
EF Core uses C# nullability to decide whether a column may be empty:
string Urlis not nullable, so the column is required (NOT NULL).string? Urlwould make the column optional.intcolumns are required.int?columns are optional.
The = "" gives each string a starting value, so the compiler does not warn that it might be null.
Common Mistakes
- No key property. If EF Core cannot find
Idor<ClassName>Id, it reports an error saying the entity requires a primary key. - Misspelled key name. A property named
BlogIDstill works, because the match ignores case. A property namedBlogNumberdoes not match. - Missing
{ get; set; }. A public field is not a property, so EF Core does not map it to a column.
Exercise
You add a class Comment with properties int CommentId, string Text, and string? Author. Which property is the primary key, and which column can be empty?
Show answer
CommentId is the primary key, because it matches the <ClassName>Id convention. Author can be empty because its type is the nullable string?. Text is required.
Exercise
Why does a class with properties int Number and string Name fail when EF Core builds the model?
Show answer
Neither property is named Id or <ClassName>Id, so EF Core finds no primary key and reports that the entity requires one.