PhoneBook 实体框架方法 C#

问题描述 投票:0回答:0

我正在做项目,当我添加一些东西然后关闭并再次打开我添加的所有东西时我遇到了问题

所有功能和按钮工作正常,搜索工作,一切似乎都很好,但我不知道为什么在按下保存按钮并关闭程序后它不保存数据,也许它只是不创建数据库文件,因为当我打开项目的文件夹时没有数据库文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PhoneBook
{
    public partial class Form1 : Form
    {
        private phonebookEntities db;

        public Form1()
        {
            InitializeComponent();
            db = new phonebookEntities();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading data: " + ex.Message);
            }
        }

        private void LoadData()
        {
            db.TablePhoneBook.Load(); 
            dataGridView1.DataSource = db.TablePhoneBook.Local.ToBindingList();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            TablePhoneBook tablePhoneBook = new TablePhoneBook()
            {
                Name = txtName.Text,
                Adress = txtAdress.Text,
                PhoneNumber = txtPhoneNumber.Text,
            };
            db.TablePhoneBook.Add(tablePhoneBook);
            dataGridView1.DataSource = db.TablePhoneBook.Local.ToBindingList();
            db.SaveChanges();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int rowIndex = dataGridView1.SelectedRows[0].Index;
                int id = Convert.ToInt32(dataGridView1.Rows[rowIndex].Cells[0].Value);

                var itemToRemove = db.TablePhoneBook.SingleOrDefault(x => x.Id == id);

                if (itemToRemove != null)
                {
                    db.TablePhoneBook.Remove(itemToRemove);
                    dataGridView1.DataSource = db.TablePhoneBook.Local.ToBindingList();
                }
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = db.TablePhoneBook
                .Where(x => x.Name == txtSearch.Text || x.Adress == txtSearch.Text)
                .Take(1)
                .ToList();
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                db.SaveChanges(); // Save changes to the database
                MessageBox.Show("Changes saved successfully.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error saving changes: " + ex.Message);
            }
        }
    }
}

c# entity-framework
© www.soinside.com 2019 - 2024. All rights reserved.