Framework and LINQ To Entities
Framework and LINQ To Entities
LINQ to Entities
LINQ to Entities provides Language Integrated Query (LINQ) support that enables
developers to write queries against Entity Framework conceptual model using Visual
Basic or Visual C#. LINQ to Entities converts Language Integrated Queries (LINQ)
queries to command-tree queries, executes the queries
LINQ to Entities also supports many other mainstream RDBMS databases such as
Oracle, DB2, and MySQL, in addition to Microsoft SQL Server.
Here I will demonstrate a sample CRUD operation (Create, Read, Update and Delete)
using entity frame work and LINQ to entities. I will explain in step by step.
1. Open Visual studio 2010 and create a new windows Form application as shown below,
Add new item -->ADO.Net Entity Data Model and name it TestDB as shown below,
Click add button. Now you will get a window as shown below.
Click next button after selecting “Generate from database”
If want to create a new connection click “New Connection” button and provide
required credentials and select the database you want to access.
5. Click the radio button “Yes, include the sensitive data in the connection string” and click
next button. Now you will get a window as shown below,
Select the tables you want to access and click finish button.
After click finish two files will be added to the project, “TestDB.edmx” and
“TestDB.Designer.cs”. The first file holds the model of entities including entity sets,
entity types, conceptual models and mappings.
Second file holds the code for model.
TestDB.Designer.cs is generated LINQ to entity class. When we open that file we can
see two classes
a. TestEntities
b. Associate
TestEntities inherit from ObjectContext class which represents the main entry point of
LINQ to entity framework.
Associate class are for table we selected. This class inherits EntityObject class which
defines all of the related property changing and property changed event methods
which we can extend to validate properties before and after the change.
{
using (TestEntities obj = new TestEntities())
FirstName = txtLastName.Text.Trim(),
LastName = txtLastName.Text.Trim(),
Email = txtEmail.Text.Trim()
};
obj.Associates.AddObject(associate);
obj.SaveChanges();
LoadGrid();
Read Records
Now we are going to query database using LINQ to entities and will bind records to
grid. To query first we need to create object of ObjectContext class or it’s sub class.
{
LoadGrid();
dgAssociate.DataSource =
First we created object of TestEntities class which inherit from ObjectContext class.
TestEntities class has a property “Associates” which holds the associate details.
When page load executed you can see that your grid is binded as shown below.
Update Record
associate.FirstName = "Rahul";
associate.LastName = "Khan";
associate.Email = "[email protected]";
obj.SaveChanges();
LoadGrid();
First we load the row we want to update, here we hardcode it as “9”. Then given the
column value we want to update then call “SaveChanges” method of ObjectContext
class. Yor can see the record with AssociateId 9 is updated as shown below,
Delete Record
To delete record/records write the method as below,
if (associatesToDelete.Count() > 0)
obj.DeleteObject(associate);
obj.SaveChanges();
LoadGrid();
Here also we first queried the record we want to delete and delete each object from that
collect and finally called “SaveChanges” method. You can see the record with
AssociateId 9 is removed as show below grid,
Here all the sql queries generated by the system. We can see the generated SQL using
sql server profiler or using ToTraceString method .