45 Next entity: Dialect 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 any entity from the list: Dialect, Publisher and Author

Let's continue with Dialect

  • 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
      • LitDialect
      • Click “Add”
  • Step #2:
    • Open LitDialect file and modify the body of the class as it is shown on the slide
    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; }
    }

Note 1:

LitDialect-class has two object-fileds:

    public LitLanguage Language { get; set; }

and

    public LitCountry Country { get; set; }

To complete master-detail declaration we have to modify LitCountry and LitLanguage classes

New declaration of LitCountry class must be as follows

public class LitCountry
{
    //[Key]
    //[Column("Iso3", Order = 0)] // zero-based order of the column the property is mapped to
    [Display(Description = "Country Iso 3 code", Name = "Iso 3 code", Prompt = "Enter Iso 3 code", ShortName = "Iso 3")]
    [StringLength(3, MinimumLength = 3)]
    [Required]
    public string Iso3 { get; set; }

    //[Key]
    //[Column("Iso2", Order = 1)]
    [Display(Description = "Country Iso 3 code", Name = "Iso 2 code", Prompt = "Enter Iso 2 code", ShortName = "Iso 2")]
    [StringLength(3, MinimumLength = 2)]
    [Required]
    public string Iso2 { get; set; }

    [Display(Description = "Country Name (no more than 30 characters in length)", Name = "Country Name", Prompt = "Enter Country Name", ShortName = "Country")]
    [StringLength(40, MinimumLength = 3)]
    [Required]
    public string CountryName { get; set; }

    public List<LitDialect> Dialects { get; set; }
}

with Dialects-property added to the declaration of LitCountry class

    public List<LitDialect> Dialects { get; set; }

New declaration of LitLanguage class must be as follows

public class LitLanguage
{
    //[Key]
    //[Column("Iso3", Order = 0)] // zero-based order of the column the property is mapped to
    [Display(Description = "Language Iso 3 code", Name = "Iso 3 code", Prompt = "Enter Iso 3 code", ShortName = "Iso 3")]
    [StringLength(3, MinimumLength = 2)]
    [Required]
    public string Iso3 { get; set; }


    //[Key]
    //[Column("Iso2", Order = 1)]
    [Display(Description = "Language Iso 3 code", Name = "Iso 2 code", Prompt = "Enter Iso 2 code", ShortName = "Iso 2")]
    [StringLength(3, MinimumLength = 2)]
    [Required]
    public string Iso2 { get; set; }


    [Display(Description = "Language Name (no more than 30 characters in length)", Name = "Language Name", Prompt = "Enter Language Name", ShortName = "Language")]
    [StringLength(27, MinimumLength = 2)]
    [Required]
    public string LanguageName { get; set; }

    public List<LitDialect> Dialects { get; set; }
}

with Dialects-property added to the declaration of LitLanguage class

    public List<LitDialect> Dialects { get; set; }

Note 2:

  • 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. CS2WPF wizards trust FluentApi more than [ForeignKey] and [InverseProperty] attributes.

⚠️ **GitHub.com Fallback** ⚠️