90 Last entity: Book with One to many relations (Wpf, Xamarin) - chempkovsky/CS2WPF-and-CS2XAMARIN GitHub Wiki
According to the Database structure (https://github.com/chempkovsky/CS2WPF-and-CS2XAMARIN/wiki/05-database-structure-for-which-we-are-creating-sample-application)
And according to the rules defined in the article "06 Development Process Cycle" (https://github.com/chempkovsky/CS2WPF/wiki/06-The-development-process-cycle)
we can continue with only entity: Book
Let's continue with Book
- Step #1:
- Run Visual Studio and Open “WpfDemo” solution
- Right Click “Literature” of the “Dm01Entity”-project
- Select “Add/Class” menu item
- In the dialog enter the name for the class
- LitBook
- Click “Add”
- Run Visual Studio and Open “WpfDemo” solution
- Step #2:
- Open LitBook file and modify the body of the class as it is shown on the slide
public class LitBook
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Description = "Row id", Name = "Id of the Book", Prompt = "Id of the Book", ShortName = "Book Id")]
public int BookId { get; set; }
[Display(Description = "Title of the Literary Book", Name = "Title of the Literary Book", Prompt = "Enter the title of the Literary Book", ShortName = "Book Title")]
[Required]
[StringLength(55, MinimumLength = 3)]
public string BookTitle { get; set; }
[Display(Description = "Publication Date", Name = "Publication Date", Prompt = "Enter Publication Date", ShortName = "Publication Date")]
public DateTime PublicationDate { get; set; }
[Display(Description = "Book Price", Name = "Book Price", Prompt = "Enter Book Price", ShortName = "Book Price")]
[DataType(DataType.Currency)]
public Double Price { get; set; }
[Display(Description = "Row id", Name = "Id of the Publisher", Prompt = "Enter Id of the Publisher", ShortName = "Publisher Id")]
[Required]
public int PublisherIdRef { get; set; }
public LitPublisher Publisher { get; set; }
[Display(Description = "Row id", Name = "Id of the Manuscript", Prompt = "Id of the Manuscript", ShortName = "Manuscript Id")]
[Required]
public int ManuscriptIdRef { get; set; }
public LitManuscript Manuscript { get; set; }
[Display(Description = "Row id", Name = "Id of the edition", Prompt = "Enter Id of the edition", ShortName = "Edition Id")]
public int? EditionIdRef { get; set; }
public LitEdition Edition { get; set; }
}
LitBook
-class has three object-fileds:
public LitPublisher Publisher { get; set; }
and
public LitManuscript Manuscript { get; set; }
and
public LitEdition Edition { get; set; }
To complete master-detail declaration we have to modify LitEdition
, LitManuscript
and LitPublisher
classes
New declaration of LitEdition class must be as follows
public class LitEdition
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Description = "Row id", Name = "Id of the edition", Prompt = "Enter Id of the edition", ShortName = "Edition Id")]
[Required]
public int EditionId { get; set; }
[Display(Description = "Name of the edition", Name = "Name of the edition", Prompt = "Enter the Name of the edition", ShortName = "Edition Name")]
[StringLength(40, MinimumLength = 3)]
public string EditionName { get; set; }
public List<LitBook> Books { get; set; }
}
with Books
-property added to the declaration of LitEdition class
public List<LitBook> Books { get; set; }
public class LitManuscript
{
// [Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Description = "Row id", Name = "Id of the Manuscript", Prompt = "Id of the Manuscript", ShortName = "Manuscript Id")]
[Required]
public int ManuscriptId { get; set; }
[Display(Description = "Manuscript Title (no more than 60 characters in length)", Name = "Manuscript Name", Prompt = "Enter Manuscript Title", ShortName = "Title")]
[StringLength(60, MinimumLength = 3)]
[Required]
public string ManuscriptTitle { get; set; }
[Display(Description = "Date Of Manuscript Completion (not Required)", Name = "Completion Date", Prompt = "Enter Completion Date", ShortName = "Completion Date")]
[DataType(DataType.Date)]
[Required]
public DateTime CompletionDate { get; set; }
[DataType(DataType.Date)]
[Display(Description = "Date Of Manuscript Beginning (not Required)", Name = "Beginning Date", Prompt = "Enter Beginning Date", ShortName = "Completion Date")]
public Nullable<DateTime> BeginningDate { get; set; }
[Display(Description = "Row id", Name = "Id of the Author", Prompt = "Enter Id of the Author", ShortName = "Author Id")]
[Required]
public int AuthorIdRef { get; set; }
public LitAuthor Author { get; set; }
[Display(Description = "Row id", Name = "Id of the genre", Prompt = "Id of the genre", ShortName = "Genre Id")]
[Required]
public int GenreIdRef { get; set; }
public LitGenre Genre { get; set; }
[StringLength(14, MinimumLength = 5)]
[Required]
public string DialectIdRef { get; set; }
public LitDialect Dialect { get; set; }
public ICollection<LitBook> Books { get; set; }
}
with Books
-property added to the declaration of LitManuscript class
public ICollection<LitBook> Books { get; set; }
New declaration of LitPublisher class must be as follows
public class LitPublisher
{
//[Key]
//[Column("PublisherId", Order = 0)] // zero-based order of the column the property is mapped to
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Description = "Row id", Name = "Id of the Publisher", Prompt = "Enter Id of the Publisher", ShortName = "Publisher Id")]
[Required]
public int PublisherId { get; set; }
[Display(Description = "Publisher Name (no more than 20 characters in length)", Name = "Publisher Name", Prompt = "Enter Publisher Name", ShortName = "Publisher Name")]
[StringLength(40, MinimumLength = 3)]
[Required]
public string PublisherName { get; set; }
[Display(Description = "Publisher Country Iso 3 code", Name = "Publisher Country Iso 3 code", Prompt = "Enter Publisher Country Iso 3 code", ShortName = "Publisher Country Iso 3")]
[StringLength(3, MinimumLength = 3)]
[Required]
public string Iso3CntrRef { get; set; }
[Display(Description = "Publisher Country Iso 2 code", Name = "Iso 2 code", Prompt = "Enter Publisher Country Iso 2 code", ShortName = "Publisher Country Iso 2")]
[StringLength(3, MinimumLength = 2)]
[Required]
public string Iso2CntrRef { get; set; }
public LitCountry Country { get; set; }
public List<LitBook> Books { get; set; }
}
with Books
-property added to the declaration of LitPublisher class
public List<LitBook> Books { get; set; }
- we are going to define entity with one-to-many relations
It's highly recommended not to use [ForeignKey] and [InverseProperty] attributes. Use Fluent Api instead. CS2REACT wizards trust FluentApi more than [ForeignKey] and [InverseProperty] attributes.