79 Next entity: Manuscript 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: Manuscript
Let's continue with Manuscript
- 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
- LitManuscript
- Click “Add”
- Run Visual Studio and Open “WpfDemo” solution
- Step #2:
- Open LitManuscript file and modify the body of the class as it is shown on the slide
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; }
}
LitManuscript
-class has three object-fileds:
public LitDialect Dialect { get; set; }
and
public LitGenre Genre { get; set; }
and
public LitAuthor Author { get; set; }
To complete master-detail declaration we have to modify LitAuthor
, LitGenre
and LitDialect
classes.
New declaration of LitAuthor class must be as follows
public class LitAuthor
{
//[Key]
//[Column("AuthorId", Order = 0)] // zero-based order of the column the property is mapped to
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Description = "Row id", Name = "Id of the Author", Prompt = "Enter Id of the Author", ShortName = "Author Id")]
[Required]
public int AuthorId { get; set; }
[Display(Description = "First Name (no more than 16 characters in length)", Name = "First Name", Prompt = "Enter First Name", ShortName = "First Name")]
[StringLength(16, MinimumLength = 3)]
[Required]
public string FirstName { get; set; }
[Display(Description = "Last Name (no more than 20 characters in length)", Name = "Last Name", Prompt = "Enter Last Name", ShortName = "Last Name")]
[StringLength(30, MinimumLength = 3)]
[Required]
public string LastName { get; set; }
[Display(Description = "Birth Date", Name = "Birth Date", Prompt = "Enter Birth Date", ShortName = "Birth Date")]
[DataType(DataType.Date)]
public Nullable<DateTime> BirthDate { get; set; }
[Display(Description = "Death Date (Required)", Name = "Death Date", Prompt = "Enter Death Date", ShortName = "Death Date")]
[DataType(DataType.Date)]
public DateTime? DeathDate { get; set; }
[Display(Description = "Birth Country Iso 3 code", Name = "Birth Country Iso 3 code", Prompt = "Enter Birth Country Iso 3 code", ShortName = "Birth Country Iso 3")]
[StringLength(3, MinimumLength = 3)]
[Required]
public string Iso3CntrRef { get; set; }
[Display(Description = "Birth Country Iso 2 code", Name = "Iso 2 code", Prompt = "Enter Birth Country Iso 2 code", ShortName = "Birth Country Iso 2")]
[StringLength(3, MinimumLength = 2)]
[Required]
public string Iso2CntrRef { get; set; }
public LitCountry Country { get; set; }
public List<LitManuscript> Manuscripts { get; set; }
}
with Manuscripts
-property added to the declaration of LitAuthor class
public List<LitManuscript> Manuscripts { get; set; }
New declaration of LitGenre class must be as follows
public class LitGenre
{
[Display(Description = "Row id", Name = "Id of the genre", Prompt = "Enter Genre Id", ShortName = "Genre Id")]
[Required]
public int GenreId { get; set; }
[Display(Description = "Name of the genre", Name = "Name of the genre", Prompt = "Enter Genre Name", ShortName = "Genre Name")]
[StringLength(20, MinimumLength = 3)]
[Required]
public string GenreName { get; set; }
public List<LitManuscript> Manuscripts { get; set; }
}
with Manuscripts
-property added to the declaration of LitGenre class
public List<LitManuscript> Manuscripts { get; set; }
New declaration of LitDialect class must be as follows
public class LitDialect
{
[Display(Description = "Dialect Id", Name = "Dialect Id", Prompt = "Dialect Id", ShortName = "Id")]
[StringLength(14, MinimumLength = 5)]
[Required]
public string DialectId { get; set; }
[Display(Description = "Dialect Name (no more than 40 characters in length)", Name = "Dialect Name", Prompt = "Enter Dialect Name", ShortName = "Dialect")]
[StringLength(52, MinimumLength = 2)]
[Required]
public string DialectName { get; set; }
[Display(Description = "Country Iso 3 code", Name = "Country Iso 3 code", Prompt = "Enter Country Iso 3 code", ShortName = "Country Iso 3")]
[StringLength(3, MinimumLength = 3)]
[Required]
public string Iso3CntrRef { get; set; }
[Display(Description = "Country Iso 2 code", Name = "Iso 2 code", Prompt = "Enter Country Iso 2 code", ShortName = "Country Iso 2")]
[StringLength(3, MinimumLength = 2)]
[Required]
public string Iso2CntrRef { get; set; }
public LitCountry Country { get; set; }
[Display(Description = "Language Iso 3 code", Name = "Iso 3 code", Prompt = "Enter Language Iso 3 code", ShortName = "Language Iso 3")]
[StringLength(3, MinimumLength = 3)]
[Required]
public string Iso3LngRef { get; set; }
[Display(Description = "Language Iso 2 code", Name = "Iso 2 code", Prompt = "Enter Language Iso 2 code", ShortName = "Language Iso 2")]
[StringLength(3, MinimumLength = 2)]
[Required]
public string Iso2LngRef { get; set; }
public LitLanguage Language { get; set; }
public List<LitManuscript> Manuscripts { get; set; }
}
with Manuscripts
-property added to the declaration of LitDialect class
public List<LitManuscript> Manuscripts { 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.