From fe9ce0ad550bbb53dc0b0e2db9347cc719ac0d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=20=C5=A0inik?= Date: Sun, 16 Jun 2024 14:28:57 +0200 Subject: [PATCH] Fix: entity tracking issue and correct method invocation --- LangLang/Repositories/SqlRepositories/CourseRepository.cs | 8 ++++++++ .../CourseView/CourseUpdateWindow.xaml.cs | 2 +- LangLang/WPF/Views/TutorView/Tabs/Courses.xaml.cs | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/LangLang/Repositories/SqlRepositories/CourseRepository.cs b/LangLang/Repositories/SqlRepositories/CourseRepository.cs index 7bb2a67..0052725 100644 --- a/LangLang/Repositories/SqlRepositories/CourseRepository.cs +++ b/LangLang/Repositories/SqlRepositories/CourseRepository.cs @@ -1,5 +1,6 @@ using LangLang.Domain.Models; using LangLang.Domain.RepositoryInterfaces; +using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; @@ -32,10 +33,17 @@ public void Add(Course course) public void Update(Course course) { + var existingCourse = _databaseContext.Course.Find(course.Id); + if (existingCourse != null) + { + _databaseContext.Entry(existingCourse).State = EntityState.Detached; + } + _databaseContext.Course.Update(course); _databaseContext.SaveChanges(); } + public void Delete(Course course) { var existingCourse = _databaseContext.Course.Find(course.Id); diff --git a/LangLang/WPF/Views/TutorView/AdditionalWindows/CourseView/CourseUpdateWindow.xaml.cs b/LangLang/WPF/Views/TutorView/AdditionalWindows/CourseView/CourseUpdateWindow.xaml.cs index 10b3dce..8dabdfe 100644 --- a/LangLang/WPF/Views/TutorView/AdditionalWindows/CourseView/CourseUpdateWindow.xaml.cs +++ b/LangLang/WPF/Views/TutorView/AdditionalWindows/CourseView/CourseUpdateWindow.xaml.cs @@ -21,7 +21,7 @@ public CourseUpdateWindow(Courses parent, Course course) CourseUpdateVM = new(course); DataContext = CourseUpdateVM; - languageLvlCb.ItemsSource = Enum.GetValues(typeof(LanguageLevel)); + languageLvlCb.ItemsSource = Enum.GetValues(typeof(Level)); } private void CourseUpdateBtn_Click(object sender, RoutedEventArgs e) diff --git a/LangLang/WPF/Views/TutorView/Tabs/Courses.xaml.cs b/LangLang/WPF/Views/TutorView/Tabs/Courses.xaml.cs index 99b91de..60283bd 100644 --- a/LangLang/WPF/Views/TutorView/Tabs/Courses.xaml.cs +++ b/LangLang/WPF/Views/TutorView/Tabs/Courses.xaml.cs @@ -63,7 +63,7 @@ private void CourseSearchWindowBtn_Click(object sender, RoutedEventArgs e) private void CourseUpdateWindowBtn_Click(object sender, RoutedEventArgs e) { - CoursesViewModel.Update(); + CoursesViewModel.UpdateCourse(); } private void CourseDeleteBtn_Click(object sender, RoutedEventArgs e)