Sunday, June 19, 2016
In my previous post we've created the Northwind products page using the SqlDataSource data control, even though there's nothing wrong with the solution. SqlDataSource is a an old technology and is not meant to be an enterprise solution. SqlDataSource is the stone age of .NET data access technology. The future of .NET is the Entity Framework.
The goal of the Entity Framework is to create a conceptual model of your data store so that you can work with tables and rows as objects, or entities. In essence the developer does not know or care what the data store is, he does not have to be a dba to work with the data.
To demonstrate the Entity Framework let's create the Northwind products page using the Entity Data Model of the Northwind database.
Here are the steps:
1. Open the Northwind project you created with the SqlDataSource
2. Righ-click on the Northwind project in "Solution Explorer", then select "Add", then "New Item"
3. Select "Data" under "Visual C#" in the "Add New Item" dialog box, then select "ADO.NET Entity Data Model"
NorthwindEntities nEntitites = new NorthwindEntities();
var products = nEntitites.Products;
GridView1.DataSource = products;
GridView1.DataBind();
}
Source Code: Northwind.zip, click "File" -> "Download" to download the source code in .zip format.
The goal of the Entity Framework is to create a conceptual model of your data store so that you can work with tables and rows as objects, or entities. In essence the developer does not know or care what the data store is, he does not have to be a dba to work with the data.
To demonstrate the Entity Framework let's create the Northwind products page using the Entity Data Model of the Northwind database.
Here are the steps:
1. Open the Northwind project you created with the SqlDataSource
2. Righ-click on the Northwind project in "Solution Explorer", then select "Add", then "New Item"
3. Select "Data" under "Visual C#" in the "Add New Item" dialog box, then select "ADO.NET Entity Data Model"
4. In the "Choose Model Contents" dialog box, choose "Generate from database", then click "Next"
5. On the "Choose Your Data Connection" dialog box, click on the "New Connection" button, then type in the name of your database server in the "Server name:" field and select the "Northwind" database in the "Connection Properties" dialog box.
6. Select the "Northwind" database connection, and save the Entity connection as "NorthwindEntities", then click "Next"
7. Choose all of the database tables in the "Choose Your Database Objects and Settings" dialog box, leave the "Model Namespace", then click "Finish". Entity Framework for the "Northwind" database model will be generated for you in Visual Studio.
8. In "Solution Explorer" there is also a "NorthwindModel.edmx" file that contains the Entity and data store mappings in raw XML.
9. Now to query the Northwind model you need to use LINQ, create a new Web Form and call it Products.aspx
10. Add a GridView to the design surface
11. Open the code-behind page Products.aspx.cs then in the Page_Load method type in the following
protected void Page_Load(object sender, EventArgs e)
{NorthwindEntities nEntitites = new NorthwindEntities();
var products = nEntitites.Products;
GridView1.DataSource = products;
GridView1.DataBind();
}
In the code above you instantiate a new NorthwindEntities model, then assign the Products collection into the variable products. The Products entity contains a collection of the Product object, it's like querying the Products table but you are working directly with the object created by the Entity Framework instead querying the database directly.
12. To run the application press F5, you will see that the GridView control has been populated with data
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Liên hệ Aivivu, đặt vé máy bay tham khảo
ReplyDeletegiá vé máy bay đi Mỹ khứ hồi
giá vé về việt nam
các chuyến bay từ đức về việt nam hôm nay
giá vé nhật việt
mở lại đường bay việt nam - hàn quốc
giá vé máy bay từ Vancouver về việt nam
ve may bay chuyen gia nuoc ngoai