Brief introduction of all Entity Framework Version


If you are going for interview then this question might be asked.

EF 3.5

1. Basic O/RM support with Database First approach.
2. It came with VS 2008 Sp1

EF 4.0

1. POCO Support
2. Lazy loading
3. Testability improvements
4. customizable code generation and the Model First approach.
5. It came with VS 2010.

EF 4.1

1. First to available of NuGet package
2. Simplified DBContext API over ObjectContext,
3. Code First approach.
4. EF 4.1.1 patch released with bug fixing of 4.1.
5. It came with VS 2010 with NuGet package.

EF 4.3

1. Code First Migrations feature that allows a database created by Code First to be incrementally changed as your Code First model evolves.
EF 4.3.1 patch released with bug fixing of EF 4.3.
2. It came with VS 2010 with NuGet package.

EF 5.0

1. Announced EF as Open Source.
2. Introduced Enum support,
3. Table-valued functions,
4. Spatial data types,
5. Multiple-diagrams per model,
6. Coloring of shapes on the design surface and batch import of stored procedures,
7. EF Power Tools and various performance improvements.
8. Precompile SP and View.
9. It came with VS 2012 with NuGet package.

EF 6.0

1. Current release EF 6.0\6.1 is latest release of Entity Framework.
2. It includes many new features related to Code First & EF designer like asynchronous query & save,
3. Connection Resiliency,
4. Dependency resolution etc.
5. DbSet.AddRange/RemoveRange
6. Better Transaction Support
7. Database command logging
8. Pluggable pluralisation and singularization service
9. It came with VS 2012 and VS 2013 with NuGet package.

Advertisements

How to do CRUD Operation using EF 6.0 in asp.net ?


EmpDetails

In EF 6.0,some old method has been repleaced with new one like instead of DeleteObject() replaced with Remove() and so many other new features has been added.

This article is the replacement of My old article.

https://chandradev819.wordpress.com/2011/05/13/how-to-do-insertupdatedelete-using-entity-framework-in-asp-net/

Step1:In aspx page design the form as per as above UI and configure the Gridview like this

 <asp:GridView ID="GridView1" runat="server" EnableViewState="true" AutoGenerateColumns="False" Width="446px" OnRowCommand="GridView1_RowCommand">
                            <Columns>
                                <asp:TemplateField HeaderText="No">
                                    <ItemTemplate>
                                        <%#Container.DataItemIndex+1 %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField HeaderText="EmpName" DataField="EmpName" />
                                <asp:BoundField HeaderText="EmpAddress" DataField="EmpAddress" />
                                <asp:TemplateField HeaderText="Action">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="EditRow"  CommandArgument='<%# Eval("Id") %>' OnClientClick="return confirm('Are you Sure?')"  CausesValidation="false" />
                                        <asp:LinkButton ID="lnlDelete" runat="server" Text="Delete" CommandName="DeleteRow" CommandArgument='<%# Eval("Id") %>' OnClientClick="return confirm('Are you Sure?')" CausesValidation="false" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                        <asp:HiddenField ID="HiddenField1" runat="server" />

Step2: Write the code in code behind like this



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EFTest.Models;

namespace EFTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadEmpData();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (btnSubmit.Text == "Submit")
            {
                SaveEmpData();
            }
            else
            {
                EditEmpData();
            }
            LoadEmpData();
        }

        private void SaveEmpData()
        {
            using (EmpDBEntities dbEntities = new EmpDBEntities())
            {
                tblEmp objEmp = new tblEmp();
                objEmp.EmpName = txtEmpName.Text;
                objEmp.EmpAddress = txtEmpAddress.Text;
                dbEntities.tblEmps.Add(objEmp);

                dbEntities.SaveChanges();
                txtEmpName.Text = string.Empty;
                txtEmpAddress.Text = string.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void EditEmpData()
        {
            using (EmpDBEntities dbEntities = new EmpDBEntities())
            {
                int id = Convert.ToInt32(HiddenField1.Value);
                tblEmp objEmp = dbEntities.tblEmps.Find(id);
                objEmp.EmpName = txtEmpName.Text;
                objEmp.EmpAddress = txtEmpAddress.Text;
                dbEntities.SaveChanges();

                txtEmpName.Text = string.Empty;
                txtEmpAddress.Text = string.Empty;
                btnSubmit.Text = "Submit";
            }
        }

        private void LoadEmpData()
        {
            using (EmpDBEntities dbEntities = new EmpDBEntities())
            {
                GridView1.DataSource = dbEntities.tblEmps.ToList();
                GridView1.DataBind();
            } 
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "DeleteRow")
            {
                using (var dbEntities = new EmpDBEntities())
                {
                    tblEmp objEmp = dbEntities.tblEmps.Find(id);
                    dbEntities.tblEmps.Remove(objEmp);
                    dbEntities.SaveChanges();
                    LoadEmpData();
                }
            }
            else if (e.CommandName == "EditRow")
            {
                using (EmpDBEntities dbEntities = new EmpDBEntities())
                {
                    tblEmp objEmp = dbEntities.tblEmps.Find(id);
                    txtEmpName.Text = objEmp.EmpName;
                    txtEmpAddress.Text = objEmp.EmpAddress;
                    HiddenField1.Value = objEmp.Id.ToString();
                    btnSubmit.Text = "Update";
                }
            }
        }
    }
}


Posted in EF 6.0. 1 Comment »