Introduction
In this article we will see how to add list of item to database using entity framework.
Step 1: Create asp.net web application
WebForm1.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Adding_List_EntityFramework.WebForm1" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="Button1" runat="server" Text="Insert Data" OnClick="Button1_Click" />
- </div>
- </form>
- </body>
- </html>
WebForm1.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace Adding_List_EntityFramework
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- EmployeeDBEntities context = new EmployeeDBEntities();
- List<Employee> employees = new List<Employee>()
- {
- new Employee{FirstName="Andy", LastName="Jones"},
- new Employee{FirstName="Steve", LastName="Robes"},
- new Employee{FirstName="Dave", LastName="Robe"},
- };
- foreach (Employee employee in employees)
- {
- context.Employees.Add(employee);
- }
- context.SaveChanges();
- }
- }
- }
Output of the application looks like this

Summary
In this blog we have seen how we can add list of items to database using entity framework. Happy coding!

usharani saribalaPosted Jul 26, 2019, 12:00 AM
By using code it's only inserting final record of the list and it's not inserting list
dhivya amrPosted Jul 11, 2018, 12:44 AM
What is EmployeeDBEntities?
hamidreza shahianPosted Oct 25, 2017, 8:59 AM
Tnx a lot.short and useful
Santhakumar MunuswamyPosted May 4, 2015, 2:29 PM
good