WPF MVVM relational database with foreign keys sample

Igor Boras 41 Reputation points
2020-12-02T08:05:03.287+00:00

Hi what is the best practice to work with foreign keys?
How to design object class?

Should I use valueConverter to switch between RoleId to RoleName

44333-image.png

44284-image.png

44365-image.png

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,688 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
768 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-12-02T18:49:59.94+00:00

    Hi Igor
    for displaying you can use readonly ComboBoxColumn like in following demo:

    XAML:

    <Window x:Class="WpfApp1.Window018"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp018"  
            mc:Ignorable="d"  
            Title="Window018" Height="450" Width="800">  
      <Window.Resources>  
        <local:ViewModel x:Key="vm"/>  
      </Window.Resources>  
      <Grid DataContext="{StaticResource vm}">  
        <DataGrid ItemsSource="{Binding Persons}" AutoGenerateColumns="false">  
          <DataGrid.Columns>  
            <DataGridTextColumn Header="ID" Binding="{Binding PersonId}"/>  
            <DataGridTextColumn Header="Firstname" Binding="{Binding FirstName}"/>  
            <DataGridComboBoxColumn Header="Role name"   
                                    IsReadOnly="True"  
                                    ItemsSource="{Binding Roles, Source={StaticResource vm}}"  
                                    SelectedValueBinding="{Binding RoleId}"  
                                    SelectedValuePath="RoleId"  
                                    DisplayMemberPath="RoleName"/>  
          </DataGrid.Columns>  
        </DataGrid>  
      </Grid>  
    </Window>  
    

    Classes:

    using System;  
    using System.Collections.Generic;  
    using System.Windows;  
      
    namespace WpfApp018  
    {  
      public class ViewModel  
      {  
        public ViewModel()  
        {  
          Random rnd = new Random();  
          for (int i = 1; i < 10; i++) colPersons.Add(new Person() { PersonId = i, FirstName = $"First name {i}", RoleId = rnd.Next(1, 5) });  
          colRoles.Add(new Role() { RoleId = 1, RoleName = "Admin" });  
          colRoles.Add(new Role() { RoleId = 2, RoleName = "User" });  
          colRoles.Add(new Role() { RoleId = 3, RoleName = "Member" });  
          colRoles.Add(new Role() { RoleId = 4, RoleName = "Owner" });  
        }  
        private List<Person> colPersons = new List<Person>();  
        private List<Role> colRoles = new List<Role>();  
        public List<Person> Persons { get => colPersons; }  
        public List<Role> Roles { get => colRoles; }  
      }  
      
      public class Person  
      {  
        public int PersonId { get; set; }  
        public string FirstName { get; set; }  
      
        public int RoleId { get; set; }  
      }  
      public class Role  
      {  
        public int RoleId { get; set; }  
        public string RoleName { get; set; }  
      }  
    }  
    

    Result:

    44532-x.png


1 additional answer

Sort by: Most helpful
  1. Igor Boras 41 Reputation points
    2020-12-04T13:32:05.06+00:00

    Hi Peter thanx for that answer.
    I have another scenario where we have another table PersonDetail
    45186-image.png

    And I want to show last valid row (history track details one person have many personDetail)....
    45237-image.png

    in this scenario we have data from 3 tables. should I create DTO witch have all data in one class (super class PersonRoleDetail) and then use join on database to retrieve all needed data?

    or is there some another way around to do this on front end side?

    I use ADO.NET with reflection to load data (not manual mapping )