Adding the DbContext to Blazor
Using DbContext
, we will be able to access the data from our database. We need to add DbContext
to our Blazor project to be able to access the data from Blazor:
- Beneath the
MyBlogServerSide
node in Solution Explorer, find Dependencies. Right-click on Dependencies and select Add Project reference. - In the list of projects, check the MyBlog.Data project and click OK:
Now we have all the external items in place, including the external NuGet packages and a reference to our MyBlog.Data project.
- In the MyBlogServerSide project, open the
Startup.cs
file and add the followingusing
statements:using Microsoft.EntityFrameworkCore; using MyBlog.Data; using MyBlog.Data.Interfaces; using MyBlogServerSide.Data;
- Add following code to the
ConfigureServices
method:services.AddDbContextFactory<MyBlogDbContext>(opt => opt.UseSqlite($"Data Source=../MyBlog.db")); services.AddScoped<...