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

Note 1:

LitPublisher-class has one object-fileds:

    public LitCountry Country { get; set; }

To complete master-detail declaration we have to modify LitCountry class

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; }
        public List<LitPublisher> Publishers { get; set; }

    }

with Publishers-property added to the declaration of LitCountry class

    public List<LitPublisher> Publishers { 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** ⚠️