69 Next entity: Author 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 Author
- 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
- LitAuthor
- Click “Add”
- Run Visual Studio and Open “WpfDemo” solution
- Step #2:
- Open LitAuthor file and modify the body of the class as it is shown on the slide
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; }
}
LitAuthor
-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; }
public List<LitAuthor> Authors { get; set; }
}
with Authors
-property added to the declaration of LitCountry class
public List<LitAuthor> Authors { 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.