Dotnet
Articles Part 1
  •  

Exercise - Modify Conduit Context

PRO
Outline

Exercise: Modify Conduit Context

While we create some of the Data Models we're going to run into the "chicken & egg" problem. This is very common when working with Object Relational Model Frameworks. I'll give you the liberty of building out these model however you feel comfortable.

In the Conduilt.Data project:

Add the Editorial Class with the following properties
Visibility | Modifier | Type                      | Name         | Rules
---------------------------------------------------------------------------------------------
  public   |          | int                       | Id           | key
  public   |          | int                       | PersonId     | Required
  public   |          | string                    | Slug         | Required, StringLenth(50)
  public   |          | string                    | Title        | Required, StringLenth(50)
  public   |          | string                    | Description  | Required, StringLenth(50)
  public   |          | string                    | Body         | Required, StringLenth(200)
  public   |          | DateTime                  | CreatedAt    | Required
  public   |          | DateTime                  | UpdatedAt    | Required
  public   |          | Person                    | Person       | 
  public   | virtual  | ICollection<Tag>          | Tags         | 
  public   | virtual  | ICollection<Commentary>   | Commentaries | 
  public   | virtual  | ICollection<Favorite>     | Favorites    | 

Create a Constructor and initialize:

  • Tags
  • Commentaries
  • Favorites

Hint: Review the code in the Person Class

Add the Commentary Class with the following properties
Visibility | Type                      | Name         | Rules
----------------------------------------------------------------------------------
  public   | int                       | Id           | key
  public   | int                       | PersonId     | Required, StringLenth(50)
  public   | int.                      | EditorialId  | Required
  public   | string                    | Body         | Required, StringLenth(200)
  public   | DateTime                  | CreatedAt    | Required
  public   | DateTime                  | UpdatedAt    | Required
  public   | Person                    | Person       | 
  public   | Editorial                 | Editorial    | 
Add the Favorite Class with the following properties
Visibility | Modifier | Type                      | Name 
---------------------------------------------------------------------------------------------
  public   |          | int                       | PersonId
  public   |          | int                       | EditorialId
  public   | virtual  | Person                    | Person
  public   | virtual  | Editorial                 | Editorial
Add the Tag Class with the following properties
Visibility | Modifier | Type                      | Name         | Rules
---------------------------------------------------------------------------------------------
  public   |          | int                       | Id           | key
  public   |          | string                    | DisplayName  | Required, StringLenth(25)
  public   | virtual  | ICollection<Editorial>    | Editorials   | 

Create a Constructor and initialize:

  • Editorials
Modify the Person Class and add the following properties
Visibility | Modifier | Type                      | Name
------------------------------------------------------------------
  public   | virtual  | ICollection<Editorial>    | Editorials
  public   | virtual  | ICollection<Commentaries> | Commentaries
  public   | virtual  | ICollection<Favorite>     | Favorites

Initialize the following properties in the Constructor:

  • Editorials
  • Commentaries
  • Favorites
Modify the ConduitContext Class add the following properties
Visibility | Type                      | Name
------------------------------------------------------------------
  public   | DbSet<Editorial>    | Editorials
  public   | DbSet<Commentaries> | Commentaries
  public   | DbSet<Favorite>     | Favorites
  public   | DbSet<Tag>          | Tags

In the OnModelCreating Method, add the following code

Table configuration for Editorial
modelBuilder.Entity<Editorial>(entity =>
{
    entity.Property(e => e.CreatedAt)
            .HasColumnType("datetime")
            .HasDefaultValueSql("(getutcdate())");

    entity.Property(e => e.UpdatedAt)
            .HasColumnType("datetime")
            .HasDefaultValueSql("(getutcdate())");

    entity.HasOne(d => d.Person)
            .WithMany(p => p.Editorials)
            .HasForeignKey(d => d.PersonId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_Articles_People");
});
Table configuration for Commentary
modelBuilder.Entity<Commentary>(entity =>
{
    entity.Property(e => e.CreatedAt)
            .HasColumnType("datetime")
            .HasDefaultValueSql("(getutcdate())");

    entity.Property(e => e.UpdateAt)
            .HasColumnType("datetime")
            .HasDefaultValueSql("(getutcdate())");

    entity.HasOne(d => d.Editorial)
            .WithMany(p => p.Commentaries)
            .HasForeignKey(d => d.EditorialId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_Commentary_Editorials");

    entity.HasOne(d => d.Person)
            .WithMany(p => p.Commentaries)
            .HasForeignKey(d => d.PersonId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_Commentaries_People");
});
Table configuration for Favorites
    modelBuilder.Entity<Favorite>(entity =>
    {
        entity.HasKey(e => new { e.PersonId, e.EditorialId });

        entity.HasOne(d => d.Editorial)
            .WithMany(p => p.Favorites)
            .HasForeignKey(d => d.EditorialId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_Favorites_Editorials");

        entity.HasOne(d => d.Person)
            .WithMany(p => p.Favorites)
            .HasForeignKey(d => d.PersonId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_Favorites_People");
});

We'll go over this code in detail to understand what it's doing.

Execute Data Migrations to update the Database Tables
  • open up a terminal by selecting [View], [Terminal] on the menu
  • In the terminal use the following commands:
    • dotnet ef migrations add Articles --project Conduit.Data --startup-project Conduit.Api
    • dotnet ef database update --project Conduit.Data --startup-project Conduit.Api
  • Check your database to see if the tables were created successfully
 

I finished! On to the next chapter