#7 Seeding the Domain Database
Saturday, December 1, 2018
In this article we will focus on adding some seed data to our domain database. We will make it so that whenever our application is started our seed method is invoked so that the data is added automatically for us if needed. We will also create the razor page that we will use to display our article.
Parts
- Part 29: Offset Pager Urls
- Part 28: Offset Pager Start
- Part 27: Mock Context Builder
- Part 26: Mock Repository
- Part 25: Mock Async
- Part 24: Picture Tag Helper
- Part 23: Img DPR Tag Helper
- Part 22: Img Responsive Tag Helper
- Part 21: Img Optimized Display
- Part 20: Img Optimization
- Part 19: Img Lazy Loading
- Part 18: Img Responsive
- Part 17: Bottom Nav
- Part 16: Main Nav Cookie
- Part 15: Main Nav Mobile
- Part 14: Main Nav Search
- Part 13: Main Nav Auth
- Part 12: Main Nav Anchors
- Part 11: Main Nav Logo
- Part 10: Search Results
- Part 9: Search Manager
- Part 8: Search Start
- Part 7: Seeding the Database
- Part 6: Domain Database
- Part 5: Emailing Exceptions
- Part 4: Mailkit
- Part 3: View Renderer
- Part 2: Upgrade to 2.1
- Part 1: Quick Start
The read page and its model
- WebUi
- Pages
- Articles
- Read.cshtml.cs
- Articles
- Pages
We will start by creating a razor page that we will use to read the articles that we are creating. The first step is to create the properties and populate them within the get method of our model (a).
Read.cshtml.cs
using System;
using WebUi.Domain;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebUi.Pages.Articles
{
public class ReadModel : PageModel
{
public string Author { get; set; }
public DateTime Created { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public void OnGet()
{
var article = new Article
{
Author = "Christopher R. Jamell",
Created = DateTime.Now.ToUniversalTime(),
Id = 1,
Title = "The Article Title"
};
Author = article.Author;
Created = article.Created;
Id = article.Id;
Title = article.Title;
}
}
}
- WebUi
- Pages
- Articles
- Read.cshtml
- Articles
- Pages
Now that we have properties to display it is time to display them. For right now we will just print everything out so that we can make sure that everything is working correctly (b).
Read.cshtml
@page
@model ReadModel
<div>@Model.Author</div>
<div>@Model.Created</div>
<div>@Model.Id</div>
<div>@Model.Title</div>
Preparing to seed our database
- WebUi
- Domain
- IDomainRepository
- Domain
For testing purposes we need to have entries added to our database and we will not want to have to make them by hand over and over. To facilitate this we are going to add a synchronous save method which starts by updating our repository interface (c).
IDomainRepository.cs
...
namespace WebUi.Domain
{
public interface IDomainRepository : IDisposable
{
...
int SaveChanges();
...
}
}
- WebUi
- Domain
- DomainRespository.cs
- Domain
With the interface being updated we now have to add the method to our repository class (d).
DomainRespository.cs
...
namespace WebUi.Domain
{
public class DomainRepository : IDomainRepository
{
...
public int SaveChanges()
{
return _domainContext.SaveChanges();
}
...
}
}
Seeding our database
- WebUi
- Domain
- DomainSeeder.cs
- Domain
Now we are ready to add test data to our domain database. We will do this by adding a static class that we will pass an instance of our repository to and it will take care of adding test article(s) to (e).
DomainSeeder.cs
using System;
using System.Linq;
namespace WebUi.Domain
{
public static class DomainSeeder
{
public static void Seed(IDomainRepository repository)
{
Articles(repository);
}
private static void Articles(IDomainRepository repository)
{
if (repository.Articles.Any())
{
return;
}
var article = new Article
{
Author = "Christopher R. Jamell",
Created = DateTime.Now.ToUniversalTime(),
Title = "The Article Title"
};
repository.Articles.Add(article);
repository.SaveChanges();
}
}
}
Time to add that seed data
- WebUi
- Startup.cs
Now that we have the class that will actually seed our database it is time to call it. We will do this when our application first starts up (f).
Startup.cs
...
namespace WebUi
{
public class Startup
{
...
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IDomainRepository domainRepository)
{
if (env.IsDevelopment())
{
...
DomainSeeder.Seed(domainRepository);
}
else
{
...
}
...
}
}
}
Seed
method within the Startup class will make sure that we
have data in our database when our application starts up.
Updating our read page
- WebUi
- Pages
- Articles
- Read.cshtml.cs
- Articles
- Pages
Now that we have data in our database we can update our read page to take advantage of it by injecting the repository into to model and making a database call (g).
Read.cshtml.cs
...
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace WebUi.Pages.Articles
{
public class ReadModel : PageModel
{
private readonly IDomainRepository _repository;
public ReadModel(IDomainRepository repository)
{
_repository = repository;
}
...
public async Task OnGetAsync()
{
var article = await _repository.Articles.FirstAsync();
Author = article.Author;
Created = article.Created;
Id = article.Id;
Title = article.Title;
}
}
}
We are not going to keep the article content in the database
- WebUi
- Articles
- Intro1.cshtml
- Articles
We will split the content of our article into two separate parts. We will have both an introduction as well as a main content part. Both of these will be separate files that we store on disk as opposed to within the database. We start by creating the intro for our article (h). For the intro we will just use the introduction for another article that I have previously written. It is important that we use actual content since we will include this along with information in the database to populate our search index in future videos.
Intro1.cshtml
By the time we are done with this article we will have created the example components for the rest of our
controls. We will also have registered them and provided access to them through an update to our navigation.
The navigation update will also include us make use of the iteration capability of pug to create our anchors
in a much more DRY fashion. We will also create a feature module that will make importing our form components
into an angular module much easier.
- WebUi
- Articles
- Content1.cshtml
- Articles
Next up is the main content of the article. For our purposes here the main content will not be indexed so we will just use some lorem text for it (i).
Content1.cshtml
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Culpa repudiandae ipsam omnis illo sit esse eius magni
cum rerum eum. Pariatur, omnis? Harum rerum nihil ullam aut voluptas ipsa libero?</p>
Adding the intro and content to our read page
- WebUi
- Articles
- Read.cshtml.cs
- Articles
Now that we have an intro and content for our article we need to add them to our read page. As usual we will start by adding a couple of properties to our model which will contain the path to both of the pages (j).
Read.cshtml.cs
...
namespace WebUi.Pages.Articles
{
public class ReadModel : PageModel
{
...
public string ContentPath { get; set; }
...
public string IntroPath { get; set; }
...
public async Task OnGetAsync()
{
...
ContentPath = $"Content{Id}.cshtml";
IntroPath = $"Intro{Id}.cshtml";
}
}
}
- WebUi
- Articles
- Read.cshtml
- Articles
With the paths added to our model we can use the partial tag helpers to display the contents of the pages on screen (k).
Read.cshtml
...
<h1>Intro</h1>
<partial name="@Model.IntroPath"/>
<h1>Content</h1>
<partial name="@Model.ContentPath"/>