Sample of LINQ to Entity
来源:互联网 发布:数据结构算法程序 编辑:程序博客网 时间:2024/05/22 14:32
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 Siteto 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!
- Sample of LINQ to Entity
- Linq to Entity CRUD
- LINQ to Entity 动态查询
- Linq To Entity 修改数据
- LINQ TO ENTITY 判断时间
- Linq to Entity 的问题
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查
- linq to Entity 左连接问题
- Linq to entity 动态生成where in
- Entity Framework vs. LINQ to SQL
- Linq to entity 3.5 左联接问题
- Linq to entity结合Code First
- Linq To Entity 多表联合查询
- LINQ to SQL和Entity Framework对比
- linq to Entity 实现 left join
- mahout学习过程记录--seq2sparse中文分词
- arcgis clip 裁剪时遇到的问题
- windows 7 : solr4.4安装进tomcat8中
- win7下不能收到窗口hook消息的问题
- Linux设备驱动程序——PCI相关知识的学习
- Sample of LINQ to Entity
- poj_1142_Smith Numbers(分解质因子)
- mysql的查询sql中使用case的例子
- Exception in thread java.lang.OutOfMemoryError: Java heap space
- Ubuntu10.04 网络问题解决记录
- 两台服务器之间建立dblink的SQL写法
- 编译环境搭建:Makefile
- nyist 202 红黑树(二叉树中序遍历)
- SpringMVC接收页面表单参数