C# > Data Access > Database Access > Entity Framework Core Basics
Defining a Model with Entity Framework Core
This snippet demonstrates how to define a simple model using Entity Framework Core with annotations to configure table and column properties. Understanding how to define your entities is the foundation of working with EF Core.
Code Example
This code defines two entities: Blog
and Post
. Blog
has properties like BlogId
(primary key), Url
(required string), and a navigation property Posts
representing related posts. Post
has properties like PostId
(primary key), Title
(required), Content
, and a foreign key BlogId
referencing the Blog
entity. The [Key]
, [Required]
, [MaxLength]
, and [ForeignKey]
attributes are Data Annotations used to configure how EF Core maps these classes to database tables and columns. Note the use of nullable reference types (`string? Description`) to indicate properties that can be null in the database.
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public class Blog
{
[Key]
public int BlogId { get; set; }
[Required]
[MaxLength(200)]
public string Url { get; set; }
public string? Description { get; set; }
public List<Post>? Posts { get; set; }
}
public class Post
{
[Key]
public int PostId { get; set; }
[Required]
[MaxLength(200)]
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
[ForeignKey("BlogId")]
public Blog Blog { get; set; }
}
Concepts Behind the Snippet
Entity Framework Core (EF Core) is an ORM (Object-Relational Mapper) that allows you to interact with databases using .NET objects. Defining models is a crucial first step. Data Annotations provide a declarative way to specify constraints and relationships. The Key
attribute designates the primary key, Required
ensures a property isn't null in the database, MaxLength
sets the maximum length for string properties, and ForeignKey
defines a relationship between entities.
Real-Life Use Case
Imagine building a blogging platform. The Blog
class represents a blog, and the Post
class represents a blog post. The relationship ensures that each post belongs to a specific blog. The data annotations help enforce data integrity and validation rules, ensuring that the data stored in the database is consistent and accurate. This structure simplifies database interactions, allowing developers to work with objects instead of writing raw SQL queries.
Best Practices
Interview Tip
Be prepared to explain the purpose of Data Annotations and how they relate to database schema design. Understand the difference between Data Annotations and Fluent API configuration. Also, be familiar with common Data Annotations like Key
, Required
, MaxLength
, and ForeignKey
.
When to Use Them
Use model definitions with EF Core whenever you need to interact with a relational database in a .NET application. They provide a structured and type-safe way to access and manipulate data. Defining models is essential when building data-driven applications like web applications, APIs, and desktop applications.
FAQ
-
What is the difference between Data Annotations and Fluent API in EF Core?
Data Annotations are attributes applied directly to your entity classes and properties, while Fluent API is a set of methods used in theOnModelCreating
method of yourDbContext
to configure the model. Fluent API provides more flexibility and control over the configuration, but Data Annotations are often simpler for basic scenarios. -
How do I create a database from these models?
You would typically use EF Core Migrations. First, define yourDbContext
class, then add a migration using theAdd-Migration
command in the Package Manager Console. Finally, apply the migration to the database using theUpdate-Database
command.