Y
Y
youngpr2020-09-19 19:40:25
SQLite
youngpr, 2020-09-19 19:40:25

Why won't wpf application start?

I decided to make a guide to the example from metanit on wpf. There are no errors, but the application does not start, passed the debugger, it gives an error:

System.Windows.Markup.XamlParseException: ""Вызов конструктора для типа "Phonebook.MainWindow", удовлетворяющего указанным ограничениям привязки, привел к выдаче исключения.": номер строки "6" и позиция в строке "9"."
SQLiteException: SQL logic error
no such table: Phones


using System.Data.Entity;

namespace Phonebook
{
    public class ApplicationContext : DbContext
    {
        public ApplicationContext():base("DefaultConnection")
        {
        }
        public DbSet<Phone> PhoneBook { get; set; }
    }
}


using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Windows;

namespace Phonebook
{
    public partial class MainWindow : Window
    {
        ApplicationContext db;
        public MainWindow()
        {
            InitializeComponent();

            db = new ApplicationContext();
            db.PhoneBook.Load();
            this.DataContext = db.PhoneBook.Local.ToBindingList();
        }
        // добавление
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            PhoneWindow phoneWindow = new PhoneWindow(new Phone());
            if (phoneWindow.ShowDialog() == true)
            {
                Phone phone = phoneWindow.Phone;
                db.PhoneBook.Add(phone);
                db.SaveChanges();
            }
        }
        // редактирование
        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            // если ни одного объекта не выделено, выходим
            if (phonesList.SelectedItem == null) return;
            // получаем выделенный объект
            Phone phone = phonesList.SelectedItem as Phone;

            PhoneWindow phoneWindow = new PhoneWindow(new Phone
            {
                Id = phone.Id,
                Position = phone.Position,
                PhoneNumber = phone.PhoneNumber,
                Name = phone.Name,
                Email = phone.Email,
                Organisation = phone.Organisation

            });

            if (phoneWindow.ShowDialog() == true)
            {
                // получаем измененный объект
                phone = db.PhoneBook.Find(phoneWindow.Phone.Id);
                if (phone != null)
                {
                    phone.Position = phoneWindow.Phone.Position;
                    phone.Name = phoneWindow.Phone.Name;
                    phone.PhoneNumber = phoneWindow.Phone.PhoneNumber;
                    phone.Email = phoneWindow.Phone.Email;
                    phone.Organisation = phoneWindow.Phone.Organisation;

                    db.Entry(phone).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
        }
        // удаление
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            // если ни одного объекта не выделено, выходим
            if (phonesList.SelectedItem == null) return;
            // получаем выделенный объект
            Phone phone = phonesList.SelectedItem as Phone;
            db.PhoneBook.Remove(phone);
            db.SaveChanges();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2020-09-19
@youngpr

You don't have a `Phones` table in your SQLite database. They themselves wrote English in white

R
Roman, 2020-09-20
@yarosroman

Explore EF migrations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question