Entity Framework is an Object Relational Mapper that translates code into SQL commands that update the tables in the database. An Entity is a class that may have properties associated to it.
To connect that class to a database, you would create a class that inherits from a Entity Framework class called DbContext to act as a bridge between the Entity classes and the database.
Entity Framework provides the ability to query data to and from the database using LINQ queries which provide a logical way to interact with the database using C#.
The best way to get Entity Framework is through the Microsoft Package Manager NuGet. Depending on the database you are using, you may want a different Entity Framework Installation than the standard Entity Framework Core. As an example, if you plan on using Sqlite, install Microsoft.EntityFrameworkCore.Sqlite.
You will also need Microsoft.EntityFrameworkCore.Design. Find that in Nuget, and install.
Go to nuget.org
and search for dotnet-ef
. Copy and paste the install command into a terminal and run it. The command should look something like this:
dotnet tool install --global dotnet-ef --version 6.0.1
In the project root directory, create a directory for data models (like Data) and add a class inside that will inherit the DbContext class. In this example, we are going to use the name DataContext. The naming conventions for models are [Column_Name]Context, so if you have a Blogging column in the database, the class name would be BloggingContext.
Data/DataContext.cs
using Microsoft.EntityFrameworkCore;
namespace API.Data;
public class DataContext : DbContext
{
// following code goes here...
}
The first step for making a model is to create a constructor. If you are using VS or Rider, you can right click the class name or somewhere in the scope of the class and generate a DbContext(DbContextOptions options)
constructor. In code editors, this may only be possible with a C# extensions plugin.
public DataContext(DbContextOptions options) : base(options) {}
These are the actual columns in the table. To add a property, just add the name of the column of type DbSet
along with the name of the Object it is associated with. You can use the shorthand version of of this property which creates a getter and setter on the same line. In an IDE/Editor, type prop and tab to autocomplete the shorthand property code.
public DbSet<AppUser> Users { get; set; }
Once you have a model, you’ll need to add the model configuration to the Startup class so that you can inject the model into other parts of the application. Start by locating the Startup class in the project directory and looking for the ConfigureServices
method. Then add DbContext
as a service specifying the derived class in the arrows and a connection string for your particular database inside the options parameter as a lambda. For this example, we are using SQLite.
Startup.cs
...
private readonly IConfiguration _config;
public Startup(IConfiguration config)
{
_config = config;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(options =>
{
options.UseSqlite(_config.GetConnectionString("DefaultConnection"));
});
...
}
...
Using the Entity Framework Command Line Tool, you can automatically create the classes needed to create the structure of the application database. Assuming you have dotnet-ef and EntityFrameworkCore.Design
installed (If not, check the Installation section above), run the following command to create the Migration classes inside Data/Migrations.
dotnet ef migrations add InitialCreate -o Data/Migrations
add-migration IntialCreate
Using the Entity Framework Command Line Tool, you can automatically create a database for the application as well. Use the following command to create the database:
dotnet ef database update
update-database