This article is a summary of a very important part of LINQ, namely, LINQ to entities. It can be used to query databases without writing any data access code.
Using LINQ to entities is very simple, and most of them are visualized operations. The detailed steps are as follows:
Step 1: create a data model:
1. Right-click "app_code" and select Add project. In the new window that appears, select "ADO. NET Entity Data Model". Enter the name and click "add. As shown in:
2. There are two options: generate from database, which is generated from the existing database, for example:
3. Next, select the database connection window. Here, click "New Connection" to create a database connection, for example:
4. Select the server name, authentication method, and database object, and click "OK" to return to the Select data connection window.
5. Click Next to go to the "choose your database objects" window, and check options such as tables, views, and stored procedures, for example:
6. Click "finish" to create the data model.
Step 2: Create an object context class object derived from the objectcontext class:
Such as code:
Northwindentities DB = new northwindentities (); // create an object context Class Object
Step 3: Call the attributes of the object context class to bind and display data.
Example:
1 public partial class linq2entitiestest: system. Web. UI. Page
2 {
3 protected void page_load (Object sender, eventargs E)
4 {
5 northwindentities DB = new northwindentities (); // create an object context Class Object
6
7 gvemployees. datasource = dB. Employees; // call the attributes of the object context class object to bind data.
8 page. databind ();
9}
10}
In this way, the data in the table employees in the database northwind can be bound to the gridview control. This is the basic usage of LINQ to entities. The next article will introduce the last application in the LINQ query, I hope you will continue to pay attention to this field. Thank you!