Javatpoint Logo
Javatpoint Logo

LINQ To Entities

LINQ to Entity means writing the LINQ queries over the entity framework object. Entity Framework generally is an ORM (Object Relational Mapper), which creates the database table as an object, and we call these objects like entities. By using these entities, we can perform any operation like insert, delete, update, etc.

Here we will show how to use LINQ with Entities. For that, we will create a new web application and make a connection with SQL Server using the Entity Framework. By using the Entity Framework, we will display the data in the grid view.

LINQ to Entities Example

First, we will create a new table "EmployeeDetails" in the database for that we execute the following query in the database and insert some dummy data to show in the application.

After executing the above query. The table will show the data, as shown below:

LINQ To Entities

Now we create a new web application. For that, we have to Go to File->Select New->Select Project, as shown in the below screenshot.

After selecting the new project, a new popup will open. From the popup, we will select Asp.Net Empty Web Application and give name as LINQToEntities and click OK to create the new web application, as shown below.

LINQ To Entities 1

To use Entity with LINQ, we need to add an ADO.NET Entity Data Model in our application. Here is the step by step process in ADO.NET Entity Data Model to import the SQL Tables and convert them into the object using the Entity Framework Database.

To add ADO.NET Entity Data Model, we will right click on the application ->Select Add->Select New Item->Select ADO.NET Entity DATA Model-> Give name as "EmployeeEntity" and click OK as shown below.

LINQ To Entities 2

After adding the ADO.NET Entity Data Model, a new popup will open, as shown in the screenshot. We already created a database with the table for our application, so we will select "EF Designer from Database" and click next, as shown below.

LINQ To Entities 3

After clicking on the Next button, following screen will appear where we need to configure our database for that click on the new connection button like as shown in the screenshot.

LINQ To Entities 4

After clicking on the New Connection button, new window will open in that choose data source "Microsoft SQL Server" and click "Continue," as shown below.

LINQ To Entities 5

After clicking on the 'Continue', connection properties window will open. Here we have to mention all the properties of the window to connect to the required database like as shown below.

LINQ To Entities 6

When all the details are filled, click "Test Connection" to test whether all the connection properties are correct or not. In case if all the information is correct, it will show the message like "Test Connection Succeeded," as shown below.

LINQ To Entities 7

When the connection is succeeded, click the OK button now; all the connection configuration will be added to our data connection, as shown below.

LINQ To Entities 8

Now click on the Next button; it will show all the database objects available in the respective database. From there, we will select the required table and click finish, as shown below.

LINQ To Entities 9

Once we click on the finish button, Entity data model will be created in our application with the required tables, as shown in the below screenshot.

LINQ To Entities 10

After adding the Entity data model, our project will look like, as shown below:

LINQ To Entities 11

In the above structure of the project, we have the context file "Model.Context.cs" it contains our database connection settings, which will look like as shown below.

LINQ To Entities 12

In the above class, we observe that it inherits from the DBContext class. In the constructor, we specified the name as "db_employeeEntities". That is why we will be using this to create the object of the context class to access the object using the Entity Framework. It also creates the "DBSet" of the type "EmployeeDetail" to interact with the database. If we want to include more tables to our Entity model, then we have to specify the "DBSet" of a new type of class.

We have another class file, "Model.cs," which contains all the properties of the "EmployeeDetail" which we will select while creating the Entity Data Model, and it looks like this.

LINQ To Entities 13

For every table which we will select from the database, a class file will be generated by the Entity Framework automatically.

Now we want to add a web page to the application. For that we have to right-click on the application-> Select Add->New Item->Select Web Form->give name as "Default1.aspx" and click "OK" button. It will create a new page in the application.

Now open the "Default1.aspx" page.

Now open the code behind the file and write the code, as shown below:

In the above example, we created the object for context file "db_employeeEntities" to access the objects of the database. Now we will run the application and see the output as shown below.

OUTPUT

LINQ To Entities 14
Next TopicLINQ to XML




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA