Ver Fonte

вывод связи многие ко многим

fly9024 há 1 mês atrás
pai
commit
37f657b66b

+ 32 - 0
AvaloniaApplication2/Models/TestBase41pContext.cs

@@ -21,6 +21,8 @@ public partial class TestBase41pContext : DbContext
 
 
     public virtual DbSet<Role> Roles { get; set; }
     public virtual DbSet<Role> Roles { get; set; }
 
 
+    public virtual DbSet<Trait> Traits { get; set; }
+
     public virtual DbSet<User> Users { get; set; }
     public virtual DbSet<User> Users { get; set; }
 
 
     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
@@ -74,6 +76,18 @@ public partial class TestBase41pContext : DbContext
                 .HasColumnName("role_name");
                 .HasColumnName("role_name");
         });
         });
 
 
+        modelBuilder.Entity<Trait>(entity =>
+        {
+            entity.HasKey(e => e.IdTrait).HasName("traits_pk");
+
+            entity.ToTable("traits");
+
+            entity.Property(e => e.IdTrait).HasColumnName("id_trait");
+            entity.Property(e => e.TraitName)
+                .HasColumnType("character varying")
+                .HasColumnName("trait_name");
+        });
+
         modelBuilder.Entity<User>(entity =>
         modelBuilder.Entity<User>(entity =>
         {
         {
             entity.HasKey(e => e.IdUser).HasName("users_pk");
             entity.HasKey(e => e.IdUser).HasName("users_pk");
@@ -100,6 +114,24 @@ public partial class TestBase41pContext : DbContext
             entity.HasOne(d => d.IdUserNavigation).WithOne(p => p.User)
             entity.HasOne(d => d.IdUserNavigation).WithOne(p => p.User)
                 .HasForeignKey<User>(d => d.IdUser)
                 .HasForeignKey<User>(d => d.IdUser)
                 .HasConstraintName("users_logined_fk");
                 .HasConstraintName("users_logined_fk");
+
+            entity.HasMany(d => d.IdTraits).WithMany(p => p.IdUsers)
+                .UsingEntity<Dictionary<string, object>>(
+                    "UserTrait",
+                    r => r.HasOne<Trait>().WithMany()
+                        .HasForeignKey("IdTrait")
+                        .OnDelete(DeleteBehavior.Restrict)
+                        .HasConstraintName("user_traits_traits_fk"),
+                    l => l.HasOne<User>().WithMany()
+                        .HasForeignKey("IdUser")
+                        .HasConstraintName("user_traits_users_fk"),
+                    j =>
+                    {
+                        j.HasKey("IdUser", "IdTrait").HasName("user_traits_pk");
+                        j.ToTable("user_traits");
+                        j.IndexerProperty<int>("IdUser").HasColumnName("id_user");
+                        j.IndexerProperty<int>("IdTrait").HasColumnName("id_trait");
+                    });
         });
         });
 
 
         OnModelCreatingPartial(modelBuilder);
         OnModelCreatingPartial(modelBuilder);

+ 13 - 0
AvaloniaApplication2/Models/Trait.cs

@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+
+namespace AvaloniaApplication2.Models;
+
+public partial class Trait
+{
+    public int IdTrait { get; set; }
+
+    public string TraitName { get; set; } = null!;
+
+    public virtual ICollection<User> IdUsers { get; set; } = new List<User>();
+}

+ 2 - 0
AvaloniaApplication2/Models/User.cs

@@ -18,4 +18,6 @@ public partial class User
     public virtual Gender IdGenderNavigation { get; set; } = null!;
     public virtual Gender IdGenderNavigation { get; set; } = null!;
 
 
     public virtual Logined IdUserNavigation { get; set; } = null!;
     public virtual Logined IdUserNavigation { get; set; } = null!;
+
+    public virtual ICollection<Trait> IdTraits { get; set; } = new List<Trait>();
 }
 }

+ 2 - 2
AvaloniaApplication2/ViewModels/MainWindowViewModel.cs

@@ -44,8 +44,8 @@ namespace AvaloniaApplication2.ViewModels
                 switch (CurrentUser.IdRole)
                 switch (CurrentUser.IdRole)
                 {
                 {
                     case 1:
                     case 1:
-                        //Page = new PageStartAdmin();
-                        Page = new PageAdminNew();
+                       Page = new PageStartAdmin();
+                      //  Page = new PageAdminNew();
                         break;
                         break;
                     case 2:
                     case 2:
                         page2VM = new Page2ViewModel(currentUser.User.IdUser);
                         page2VM = new Page2ViewModel(currentUser.User.IdUser);

+ 1 - 1
AvaloniaApplication2/ViewModels/Page2ViewModel.cs

@@ -23,7 +23,7 @@ namespace AvaloniaApplication2.ViewModels
         public Page2ViewModel(int id) 
         public Page2ViewModel(int id) 
         {
         {
             GendersList = db.Genders.ToList();
             GendersList = db.Genders.ToList();
-            editedUser = db.Users.Include(x=>x.IdGenderNavigation).FirstOrDefault(x=>x.IdUser == id);
+            editedUser = db.Users.Include(x=>x.IdGenderNavigation).Include(x=>x.IdTraits).FirstOrDefault(x=>x.IdUser == id);
             imageUser = EditedUser.Image != null ? new Bitmap(new MemoryStream(editedUser.Image)) : new Bitmap("Assets/заглушка.jpg");
             imageUser = EditedUser.Image != null ? new Bitmap(new MemoryStream(editedUser.Image)) : new Bitmap("Assets/заглушка.jpg");
         }
         }
         public DateTimeOffset DateTimeOffset
         public DateTimeOffset DateTimeOffset

+ 1 - 1
AvaloniaApplication2/ViewModels/PageStartAdminViewModel.cs

@@ -24,7 +24,7 @@ namespace AvaloniaApplication2.ViewModels
         public int ID => 1;
         public int ID => 1;
       public PageStartAdminViewModel() 
       public PageStartAdminViewModel() 
         {
         {
-            users = db.Users.Include(x => x.IdUserNavigation.IdRoleNavigation).ToList();
+            users = db.Users.Include(x => x.IdUserNavigation.IdRoleNavigation).Include(x=>x.IdTraits).ToList();
             genders =
             genders =
             [
             [
                 new Gender() { Gender1 = "Сбросить фильтр", IdGender = 0 },
                 new Gender() { Gender1 = "Сбросить фильтр", IdGender = 0 },

+ 23 - 15
AvaloniaApplication2/Views/Page2.axaml

@@ -15,22 +15,30 @@
 			<Setter Property="Background" Value="Red"/>
 			<Setter Property="Background" Value="Red"/>
 		</Style>
 		</Style>
 	</UserControl.Styles>
 	</UserControl.Styles>
-	<StackPanel>
-		<Image Source="{Binding Page2VM.ImageUser}" Width="100" Height="100"/>
-		<TextBox Text="{Binding Page2VM.EditedUser.Name}" />
-		<TextBox Text="{Binding Page2VM.EditedUser.BirthDate}"/>		
-		<!--<DatePicker SelectedDate="{Binding Page2VM.DateTimeOffset}"/>-->
-		<DatePicker SelectedDate="{Binding Page2VM.EditedUser.BirthDate, Converter={StaticResource myDateConverter}}"/>
-		<ComboBox ItemsSource="{Binding Page2VM.GendersList}" SelectedItem="{Binding Page2VM.EditedUser.IdGenderNavigation}">
-			<ComboBox.ItemTemplate>
+	<StackPanel Orientation="Horizontal">
+		<StackPanel>
+			<Image Source="{Binding Page2VM.ImageUser}" Width="100" Height="100"/>
+			<TextBox Text="{Binding Page2VM.EditedUser.Name}" />
+			<TextBox Text="{Binding Page2VM.EditedUser.BirthDate}"/>		
+			<!--<DatePicker SelectedDate="{Binding Page2VM.DateTimeOffset}"/>-->
+			<DatePicker SelectedDate="{Binding Page2VM.EditedUser.BirthDate, Converter={StaticResource myDateConverter}}"/>
+			<ComboBox ItemsSource="{Binding Page2VM.GendersList}" SelectedItem="{Binding Page2VM.EditedUser.IdGenderNavigation}">
+				<ComboBox.ItemTemplate>
+					<DataTemplate>
+						<TextBlock Text="{Binding Gender1}"/>
+					</DataTemplate>
+				</ComboBox.ItemTemplate>
+			</ComboBox>
+			<Button Content="Выбрать изображение пользователя" Command="{Binding Page2VM.Image}"/>
+			<Button Content="Сохранить изменения" Command="{Binding Page2VM.Save}"/>
+			<Button Content="Развойти обратно" Command="{Binding Page2VM.GoBack}"/>
+		</StackPanel>
+		<ListBox ItemsSource="{Binding Page2VM.EditedUser.IdTraits}">
+			<ListBox.ItemTemplate>
 				<DataTemplate>
 				<DataTemplate>
-					<TextBlock Text="{Binding Gender1}"/>
+					<TextBlock Text="{Binding TraitName}"/>
 				</DataTemplate>
 				</DataTemplate>
-			</ComboBox.ItemTemplate>
-		</ComboBox>
-		<Button Content="Выбрать изображение пользователя" Command="{Binding Page2VM.Image}"/>
-		<Button Content="Сохранить изменения" Command="{Binding Page2VM.Save}"/>
-		<Button Content="Развойти обратно" Command="{Binding Page2VM.GoBack}"/>
+			</ListBox.ItemTemplate>
+		</ListBox>
 	</StackPanel>
 	</StackPanel>
-	
 </UserControl>
 </UserControl>

+ 23 - 13
AvaloniaApplication2/Views/PageStartAdmin.axaml

@@ -33,20 +33,30 @@
 			<ListBox ItemsSource="{Binding Users}" Grid.Row="1">
 			<ListBox ItemsSource="{Binding Users}" Grid.Row="1">
 				<ListBox.ItemTemplate>
 				<ListBox.ItemTemplate>
 					<DataTemplate>
 					<DataTemplate>
-						<StackPanel>
-							<TextBlock Text="{Binding Name, StringFormat=Имя: {0}}"/>
-							<TextBlock Text="{Binding BirthDate,StringFormat=Дата рождения: {0:dd/MM/yyyy}}"/>
-							<TextBlock Text="{Binding IdGenderNavigation.Gender1, StringFormat=Пол: {0}}"/>
-							<TextBlock Text="{Binding IdUserNavigation.IdRoleNavigation.RoleName, StringFormat=Роль в системе: {0}}"/>
-							<Button Content="Изменить персональные данные пользователя" Command="{Binding $parent[UserControl].((vm:PageStartAdminViewModel)DataContext).Press}" CommandParameter="{Binding IdUser}">
-								<Button.IsVisible>
-									<MultiBinding Converter="{StaticResource myUserLogicalConverter}">
-										<Binding Path="IdUser"/>
-										<Binding Path="((vm:PageStartAdminViewModel)DataContext).ID" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=UserControl}"/>
-									</MultiBinding>
-								</Button.IsVisible>
-							</Button>
+						<StackPanel Orientation="Horizontal">
+							<StackPanel>
+								<TextBlock Text="{Binding Name, StringFormat=Имя: {0}}"/>
+								<TextBlock Text="{Binding BirthDate,StringFormat=Дата рождения: {0:dd/MM/yyyy}}"/>
+								<TextBlock Text="{Binding IdGenderNavigation.Gender1, StringFormat=Пол: {0}}"/>
+								<TextBlock Text="{Binding IdUserNavigation.IdRoleNavigation.RoleName, StringFormat=Роль в системе: {0}}"/>
+								<Button Content="Изменить персональные данные пользователя" Command="{Binding $parent[UserControl].((vm:PageStartAdminViewModel)DataContext).Press}" CommandParameter="{Binding IdUser}">
+									<Button.IsVisible>
+										<MultiBinding Converter="{StaticResource myUserLogicalConverter}">
+											<Binding Path="IdUser"/>
+											<Binding Path="((vm:PageStartAdminViewModel)DataContext).ID" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=UserControl}"/>
+										</MultiBinding>
+									</Button.IsVisible>
+								</Button>
+							</StackPanel>
+							<ListBox ItemsSource="{Binding IdTraits}">
+								<ListBox.ItemTemplate>
+									<DataTemplate>
+										<TextBlock Text="{Binding TraitName}"/>
+									</DataTemplate>
+								</ListBox.ItemTemplate>
+							</ListBox>
 						</StackPanel>
 						</StackPanel>
+						
 					</DataTemplate>
 					</DataTemplate>
 				</ListBox.ItemTemplate>
 				</ListBox.ItemTemplate>
 			</ListBox>		
 			</ListBox>