Sample of LINQ to Entity

1. Sample of LINQ to Entity

1.1 Create ASP.NET Web Application

1. Start Microsoft Visual Studio 2010 IDE.

        Then In the menu, Click File -> New -> Web Site to Open New Web Site dialog, as Fig.1 shows:

                                                                Fig.1   Add Web Project

2. Select theASP.NET Web Application Template, then the language is C sharp. Here, Our test Web Application name is WebApplication2, then Click Ok.

1.2 Add Entity Data Model

1. View the Solution Explorer, In the project of WebApplication2, right Click the web node, then Add New Item-> Data->Select the ADO.NET  Entity Data Model, as Fig.2 shows.

                                                                                Fig.2Add Entity Data Model

2. Choose ADO.NET Entity Data Model as the target template, then you can type a new name. Here, we keep the default name Model2.edmx. Click Add, then appear a new dialog, as Fig3. Shows:

                                                     Fig3.Entity Data Model Wizard

3.Click next, then another dialog appears. You can click the New Connection.., then Connect to your target database. There are two radio button labeled by Yes and No:

Ø  Yes means to include the sensitive data such as password in the connection string

Ø  No means to exclude the sensitive data such as password in the connection string

Then you can save entity connection settings in Web.Config as a name you favor. Here, we type the name as , as Fig4. Shows:

 

                                                  Fig4.Choose Data Connection

4.Click the next, In the TreeComboBox, you can choose your database objects. Here, we choose three tables: Student, Class, Student Class; then Click Finish Button.

As Fig5.Shows:

                                                          Fig 5. Choose Database Objects

1.3 Edit Entity Data Model

1. View solution Explorer, then Open Model2.edmx, then see three entities: Students, Classes, Student Classes. Pitch on the Students Entities, right click ->property, Set [Entity Set Name] as Students, Set [Name] as Student

2. Likewise, Set Classes, Student Classes as the following table:

Entity

Entity Set Name

Name

Student Classes

Student Classes

Student Class

Classes

Classes

Class

  3.  Then we can also see relations among these tables, such as m: n.

1.4 View the Generating Code

1. In Solution Explorer, Open Model2.edmx.Then double click the file of Model2.Designer.cs. We can see the Source Code. As Fig.6 Shows:

                                    Fig.6 Source Code

2. From generating codes, we can see four classes:

Ø  TestEntities1

Ø  Student

Ø  Student Class

Ø  Class

Classes of Student, Student Class and Class are defined in the Model2 Studio, their name are same with the property [name] Of Entity. The TestEntities1 is entity set which is to access to the underlying database, includes three entities: Students, Student Classes, Classes. These are same with the Property [Entity Set Name] of Entity.

1.5 Edit simple Examples of LINQ to Entity

1. In Solution Explorer, Double click Default.aspx; In the Opened Web Editor, click the Design Button to design Web page

2. Open Toolbox panel, Expand Data Node, Pull GridView control into web page.

3. In Solution Explorer Panel,Right Click the Default.aspx,Choose View Code to edit the C# Code.

4. In the method of Page_Load,type the below codes to bind data to GridView control,

TestEntities1 Entities = new TestEntities1();

       var students = from Student inEntities.Students

                          select new

                          {

                             sid =  student.SId,

                             sname =student.SName,

                             aname = student.AddressName

                          };

            this.GridView1.DataSource = students.Take(10);

            this.GridView1.DataBind();

 

5. Run the project, you can see the Search Result;

         Over!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值