Dotnet
User Profile
  •  

Exercise - Add Followers

PRO
Outline

Exercise: Adding Followers

In the Conduit.Data Project

Add a new class named Follow with the following properties:
Visibility     |     Modifier     |     Type     |   Name
-------------------------------------------------------------------------
  public       |                  |     int      |   Follower
  public       |                  |     int      |   Following
  public       |     virtual      |    Person    |   FollowerNavigation
  public       |     virtual      |    Person    |   FollowingNavigation
The second file you'll modify is Person class by adding the following properties:
Visibility     |     Modifier     |    Type                    |   Name
--------------------------------------------------------------------------------------
  public       |     virtual      |    ICollection<Follows>    |   FollowerNavigation
  public       |     virtual      |    ICollection<Follows>    |   FollowingNavigation
Also in the Person Class, create a constructor then add the following lines of code:
FollowerNavigations = new HashSet<Follow>();
FollowingNavigations = new HashSet<Follow>();

Finally the third file in the Conduit.Data Project

Modify ConduitContext by adding the following property:
Visibility   |   Type             |     Name
---------------------------------------------------
  public     |   DbSet<Follow>    |     Follows
In the OnModelCreating method, add the following code immediately after modelBuilder.Entity<Person>() configuration call.
modelBuilder.Entity<Follow>(entity =>
{
    entity.HasKey(e => new { e.Follower, e.Following });
 
    entity.HasOne(d => d.FollowerNavigation)
        .WithMany(p => p.FollowerNavigations)
        .HasForeignKey(d => d.Follower)
        .OnDelete(DeleteBehavior.ClientSetNull)
        .HasConstraintName("FK_Follower_Profiles");
 
    entity.HasOne(d => d.FollowingNavigation)
        .WithMany(p => p.FollowingNavigations)
        .HasForeignKey(d=>d.Following)
        .OnDelete(DeleteBehavior.ClientSetNull)
        .HasConstraintName("FK_Following_Profiles");
});

We'll cover this code in detail in the next video.

 

I finished! On to the next chapter