-
Notifications
You must be signed in to change notification settings - Fork 0
009 Create(modify) DBContext for the first entity (PhoneType)
- Run DBContext Wizard
- First page of DBContext Wizard
- Second page of DBContext Wizard
- Third page of DBContext Wizard
- Fourth page of DBContext Wizard
- Fifth page of DBContext Wizard
- Second page of DBContext Wizard again
- Sixth page of DBContext Wizard
- Primary Key page of DBContext Wizard
- Unique Key page of DBContext Wizard
- Second page of DBContext Wizard third time
The DBContext Wizard is a tool that creates or modifies an existing DBContext file. There are a lot of such tools. And the question is, "why was it created."
- The answer is as follows:
- Other wizards (ModelViews, WebApiServices) parse the DBContext file and Entities files for their own needs. But the task of creating full-feature parser is a time consuming. On the other hand, only a few fluent api constructs are used by ModelViews, WebApiServices wizards. So, instead of creating a fully functional parser, I created a small code generator for a very short list of constructs. According to (https://www.entityframeworktutorial.net/efcore/configure-one-to-many-relationship-using-fluent-api-in-ef-core.aspx) some fluent api designs can be done in more than one way. To make sure that the ModelViews and WebApiServices wizards parse the fluent api structures correctly, a DBContext Wizard was developed. So, using DBContext Wizard is absolutely necessary. It generates fluent api constructs in a way that ModelViews and WebApiServices wizards parse correctly.
- Right Click
PhBk
-folder of the “PhBkContext”-project and Select “DBContext Wizard” menu item to open the Wizard dialog
Click to show the picture
The first page of the wizard displays information about the target project, namespace, and folder. Click Next
-button
Click to show the picture
So far we don't have a Dbcontext file yet and we need to create it. On the Second page type PhbkDbContext
in the DbContext-edit box and click Create DBContext
-button
Click to show the picture
On the Third page choose DbContext.NET.Core.cs.t4
-template and click Next
-button
Click to show the picture
The fourth page of the wizard shows the t4 code that will be used to create the DBContext file. Click Next
-button.
Click to show the picture
The Fifth page of the wizard shows generated code. Click Save
-button.
Click to show the picture
The PhbkDbContext.cs
file will be created in the PhBk
folder of the PhBkContext
project
Click to show the file
using Microsoft.EntityFrameworkCore;
using System;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
namespace PhBkContext.PhBk
{
public class PhbkDbContext : DbContext
{
//////////////////////////////////////////////////////////////////
/// reset ".UseSqlServer"
/// reset "ConfigurationManager.ConnectionStrings"
/// optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
/////////////////////////////////////////////////////////////////
// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
// {
// optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
// }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
}
Now on the fifth page click Next
-button. It will return on to second page of the Wizard
After adding the PhbkDbContext.cs
file to the project, on the second page of the wizard, select PhBkContext.csproj and the PhBkContext class using the drop-down lists. Click Next
-button
Click to show the picture
On the Sixth page of the Wizard choose PhBkEntity.csproj
and PhbkPhoneType
-class and click Add Required property to Db-context
-button
Click to show the picture
It will add the following lines of code to the PhbkDbContext.cs file.
public DbSet<PhbkPhoneType> PhbkPhoneTypeDbSet {
get => Set<PhbkPhoneType>();
}
In addition the content of the second page of the Wizard has changed. Now it shows the message about the primary Key of the entity. Click Modify
-button. (It will show Primary Key page of DBContext Wizard.)
Click to show the picture
- On the Primary Key page
- choose
PhoneTypeId
-property to be used as a primary key, - choose
HasKey.Net.Core.cs.t4
-script to generate the code - click
Create(modify)
-button
- choose
Click to show the picture
It modifies OnModelCreating
-method of the DbContext class as shown below:
Click to show the code
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PhbkPhoneType>().HasKey(p => p.PhoneTypeId);
}
Click Next
-button to navigate to Unique Key page of DBContext Wizard
We do not require additional unique keys for the PhbkPhoneType
-entity, so click Next
-button on this page. It will navigate to the second page of the wizard.
Click to show the picture
Click Cancel
-button to close DBContext Wizard