Outline
Solution: Modify Conduit Context (Part 1)
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
Here's the code to Editorial.cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Conduit.Data
{
public class Editorial
{
[Key]
public int ID { get; set; }
[Required]
public int PersonId { get; set; }
[Required]
[StringLength(50)]
public string Slug { get; set; }
[Required]
[StringLength(50)]
public string Title { get; set; }
[Required]
[StringLength(50)]
public string Description { get; set; }
[Required]
[StringLength(50)]
public string Body { get; set; }
[Required]
public DateTime CreatedAt { get; set; }
[Required]
public DateTime UpdateAt { get; set; }
public Person Person { get; set; }
public ICollection<Tag> Tags { get; set; }
public ICollection<Commentary> Commentaries { get; set; }
public ICollection<Favorite> Favorites { get; set; }
public Editorial()
{
Tags = new HashSet<Tag>();
Commentaries = new HashSet<Commentary>();
Favorites = new HashSet<Favorite>();
}
}
}
Add the Commentary Class with the following properties
Visibility | Type | Name | Rules
----------------------------------------------------------------------------------
public | int | Id | key
public | int | PersonId | Required
public | int. | EditorialId | Required
public | string | Body | Required, StringLenth(200)
public | DateTime | CreatedAt | Required
public | DateTime | UpdatedAt | Required
public | Person | Person |
public | Editorial | Editorial |
Here's the code for the Commentary.cs file:
using System;
using System.ComponentModel.DataAnnotations;
namespace Conduit.Data
{
public class Commentary
{
[Key]
public int Id { get; set; }
[Required]
public int PersonId { get; set; }
[Required]
public int EditorialId { get; set; }
[Required]
[StringLength(200)]
public string Body { get; set; }
[Required]
public DateTime CreateAt { get; set; }
[Required]
public DateTime UpdatedAt { get; set; }
public Person Person { get; set; }
public Editorial Editorial { get; set; }
}
}
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
Here's the code to the Favorite.cs file:
namespace Conduit.Data
{
public class Favorite
{
public int PersonId { get; set; }
public int EditorialId { get; set; }
public Person Person { get; set; }
public Editorial Editorial { get; set; }
}
}
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
Here's the code to the Tag.cs file:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Conduit.Data
{
public class Tag
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string DisplayName { get; set; }
public ICollection<Editorial> Editorials { get; set; }
public Tag()
{
Editorials = new HashSet<Editorial>();
}
}
}
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
Here's the code for the Person.cs file:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Conduit.Data
{
[Table("People")]
public class Person
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string UserName { get; set; }
[StringLength(200)]
public string Bio { get; set; }
[Required]
[StringLength(200)]
public string Image { get; set; }
public Account Account { get; set; }
public virtual ICollection<Follow> FollowerNavigations { get; set; }
public virtual ICollection<Follow> FollowingNavigations { get; set; }
public ICollection<Editorial> Editorials { get; set; }
public ICollection<Commentary> Commentaries { get; set; }
public ICollection<Favorite> Favorites { get; set; }
public Person()
{
FollowerNavigations = new HashSet<Follow>();
FollowingNavigations = new HashSet<Follow>();
Editorials = new HashSet<Editorial>();
Commentaries = new HashSet<Commentary>();
Favorites = new HashSet<Favorite>();
}
}
}