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!
219

被折叠的 条评论
为什么被折叠?



