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 fetch data from StoreProcedure using Entity Framework ?


Hi

If you will create the Data model in EF using general approach then you will be not able to fetch data from SP in EF.

For fetching data from SP using EF. we can do like this

Step 1 : Add the SP and Table Name in Data Model like this

EF1

Step 2: In the data model designer, right-click on Model Browser then go to storeprocedure

EF2

Step 3: click on “Add Function Import” and configure the SP maping like image

EF3

Step 4: Click on save all and build the application

Step 5: Write the code in code behind file like this


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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (DatabaseModel.DatabaseEntities dc = new DatabaseModel.DatabaseEntities())
        {
            //var query = dc.VW_tblEmp.Select(m => m);
            var query = dc.GetEmpName();
            Grid1.DataSource = query;
            Grid1.DataBind();
        }
    }
}

Note: Here i hope that you are aware of doing configuration on model in EF.

Precompile query is available in EF 5.0

How to fetch data on basis of multiple Id in LINQ or EF ?


Hi
Recently one of my project, there was requirement to filter from one table on basis of multiple Id. There was using EF. So in EF we can write the Syntax like this

using (DemoJobPortalModel.DemoJobPortalEntities objE = new DemoJobPortalModel.DemoJobPortalEntities())
{
List<int> ListCountryId = new List<int> { 3, 5, 7 };

var query = from m in objE.tblCountries
where ListCountryId.Contains(m.Id)
select m;
GridView1.DataSource = query;
GridView1.DataBind();
}

In Sql Server we can do like this

Select *from tblCountry where Id in (3,5,7)

How to do joining of two table in EF ?


Hi
So many time we will get situation to join two table on basis of some condition. If we have to do inner join in Entity Framework then we can do like this

using (TestEntities TE = new TestEntities())
{
var query = from m in TE.tblEmps
join d in TE.tblDepts on m.Id equals d.Id
select new {m.EmpName,d.DeptName};

GridView1.DataSource = query;
GridView1.DataBind();

}

How to do insert/Update/Delete using Entity Framework in asp.net ? Part #3


Hi
I hope that you are knowing the database mapping concept of EF. If not then please see my previous post How to fill gridview using EF?

For doing CRUD operation using EF, we have to do like this
srep1: Take gridview,Lable,Hidden field asp.net control and design the page like above

<div>

<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" AllowPaging="true"
CellPadding="4" ForeColor="#333333" GridLines="None" Width="417px"
onpageindexchanging="Gridview1_PageIndexChanging"
onrowcommand="Gridview1_RowCommand"
onrowdeleting="Gridview1_RowDeleting" onrowediting="Gridview1_RowEditing">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:TemplateField HeaderText="SI.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="EmpName" DataField="EmpName" />
<asp:BoundField HeaderText="EmpSal" DataField="EmpSal" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("Id") %>’ OnClientClick="return confirm(‘Are you sure?’)" />
<asp:LinkButton ID="LinkDelete" runat="server" Text="Delete" CausesValidation="false" CommandName="Delete" CommandArgument='<%# Eval("Id") %>’ OnClientClick="return confirm(‘Are you sure?’)" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />

</asp:GridView>
<asp:HiddenField ID="HiddenField1" runat="server" />

</div>

Step2: Write the code in code behind page like this

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

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
fillGrid();
}

//This code is used for fetch data from database
protected void fillGrid()
{
using (TestModel.TestEntities TE = new TestModel.TestEntities())
{
var query = from m in TE.tblEmp
orderby m.Id descending
select m;
Gridview1.DataSource = query;
Gridview1.DataBind();
}
}

protected void BtnSubmit_Click(object sender, EventArgs e)
{
if (BtnSubmit.Text == "Submit")
{
SaveEmpData();

fillGrid();
}
else if (BtnSubmit.Text == "Update")
{
UpdateEmpRecord();
fillGrid();

}

}

//This code is used for Update functionality
private void UpdateEmpRecord()
{
using (TestModel.TestEntities TE = new TestModel.TestEntities())
{
int Id1=Convert.ToInt32(HiddenField1.Value);
var query = (from m in TE.tblEmp
where m.Id==Id1
select m).First();

query.EmpName = txtEmpName.Text;
query.EmpSal = txtEmpSal.Text;
TE.SaveChanges();
lblmsg.Text = "Data has been updated Sucessfully";
BtnSubmit.Text = "Submit";
txtEmpName.Text = "";
txtEmpSal.Text = "";
}

}

// This code is used for save functionality
private void SaveEmpData()
{
using (TestModel.TestEntities TE = new TestModel.TestEntities())
{

TestModel.tblEmp objEmp = new TestModel.tblEmp();
objEmp.EmpName = txtEmpName.Text;
objEmp.EmpSal = txtEmpSal.Text;
TE.AddTotblEmp(objEmp);
TE.SaveChanges();
lblmsg.Text = "Data has been inserted sucessfully";
txtEmpName.Text = "";
txtEmpSal.Text = "";
}

}

protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// This code is used for Delete functionality

if (e.CommandName == "Delete")
{
using(TestModel.TestEntities TE=new TestModel.TestEntities())
{
int id1 = Convert.ToInt32(e.CommandArgument);
var user = TE.tblEmp.First(m => m.Id == id1);
TE.DeleteObject(user);
TE.SaveChanges();
fillGrid();
lblmsg.Text = "";
txtEmpName.Text = "";
txtEmpSal.Text = "";

}

}

//This code is used for fetching particular row of gridview and dispaly on
// respective textbox field

else if (e.CommandName == "Edit")
{
using (TestModel.TestEntities TE = new TestModel.TestEntities())
{
int id1 = Convert.ToInt32(e.CommandArgument);
var Empinfo = GetEmpInfo(id1);
txtEmpName.Text= Empinfo[0].EmpName.ToString();
txtEmpSal.Text = Empinfo[0].EmpSal.ToString();
HiddenField1.Value= Empinfo[0].Id.ToString();
BtnSubmit.Text = "Update";
}
}

}
protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

}
protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e)
{

}
//This code is used for paging
protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Gridview1.PageIndex = e.NewPageIndex;
fillGrid();

}

private List<TestModel.tblEmp> GetEmpInfo(int Id)
{
using (TestModel.TestEntities TE = new TestModel.TestEntities())
{
return (from m in TE.tblEmp
where m.Id == Id
select m).ToList();
}
}

}

How to fill Gridview using Entity Framework in asp.net ?Part #2



Hi
Here is some few steps to do this task

Note: I have tested this code with asp.net 3.5 sp1, In asp.net 4.0 also process is same.

Step1: Go to solution Explore, then right click then add new item like this

Step2: Click on next like this

Step3: Select the Exact database and click on next like this

Step4: Select your table name like this

Step5: Then you will get Model.edmx file like this

Step6:Take one gridview asp.net control in default page and write the code in code behind page like this

protected void Page_Load(object sender, EventArgs e)
{
fillGrid();
}

//This code is used for fetch data from database
protected void fillGrid()
{
using (TestModel.TestEntities TE = new TestModel.TestEntities())
{
var query = from m in TE.tblEmp
orderby m.Id descending
select m;
Gridview1.DataSource = query;
Gridview1.DataBind();
}
}