In this post, I'll show you how to Bind Add Update Delete data using Entity Framework with Database First in C#.

We will use Northwind database to practice this example, you should read Getting Started with Entity Framework Database First to know how to add an entity framework model to your project.

Design you windows form application as shown below. You need to use Label, TextBox, Button and DataGridView control to layout your windows forms, then add a bindingsource to DataGridView and TextBox control.

As you know, the BindingSource component is designed to simplify the process of binding controls to an underlying data source.

c# entity framework

Add code to handle your windows forms as the following.

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 PdfForm
{
    public partial class frmEF : Form
    {
        public frmEF()
        {
            InitializeComponent();
        }

        NorthwindEntities db;
        private void frmEF_Load(object sender, EventArgs e)
        {
            db = new NorthwindEntities();
            db.Customers.Load();
            customerBindingSource.DataSource = db.Customers.Local;
        }

        /// <summary>
        /// Add
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {            
            customerBindingSource.AddNew();
        }

        /// <summary>
        /// Cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            var changedEntries = db.ChangeTracker.Entries().Where(x => x.State != EntityState.Unchanged).ToList();
            foreach (var entry in changedEntries)
            {
                switch (entry.State)
                {
                    case EntityState.Modified:
                        entry.CurrentValues.SetValues(entry.OriginalValues);
                        entry.State = EntityState.Unchanged;
                        break;
                    case EntityState.Added:
                        entry.State = EntityState.Detached;
                        break;
                    case EntityState.Deleted:
                        entry.State = EntityState.Unchanged;
                        break;
                }
            }
            customerBindingSource.ResetBindings(false);
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure want to delete this record?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                customerBindingSource.RemoveCurrent();
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            db.SaveChanges();
            MessageBox.Show("Your data has been successfully saved !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void frmEF_FormClosing(object sender, FormClosingEventArgs e)
        {
            db.Dispose();
        }
    }
}

Use db.Customers.Load() to load data to bindingsource control, to save data include add, update, delete you can use db.SaveChanges()

Run you project

c# insert update delete entity framework

We finished all the functions and finally, we created a simple Add, Update, Delete in Windows Form Application using Entity Framework Database First.