LINQ learning notes (10) LINQ to entities-you can query the database without writing any data access code.

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.