Are you ready?
↓2012-2-5 13:49:42
Environment:
Operating system: Microsoft window 7 flagship ...
Development tools: Visual Studio 2010 flagship ...
VS extension: NuGet Package Manager Latest (my current version is 1.6.21215.9133)
Database: Microsoft SQL Server (my current version is SQL Server 2005)
↓2012-2-5 13:49:54
A blah .
The series should also be a patchwork, is currently in order to comb their understanding of the Entity Framework for their own has always wanted to enter the WPF, MVC, Silverlight, WP7 dozen Good solid foundation.
At present, there are three kinds of construction methods in the Entity Framework: 1. Database first;2. Model first;3. Code A.
Therefore, this article will successively create several databases, respectively named EF.DF, EF. MF, EF. Cf.
walkthroughs, however, are simpler than WinForm, WebForm, WPF, MVC, Silverlight, etc., or console applications ...
For the same purpose as the namespace, the solution that you created is named EF
Creating a console application-- EF (screenshot is wrong, name of sample removed)
List of items (abbreviated)
Creating a database
CREATE DATABASE [EF. DF]
Build database table User, UserInfo
table User (alas, the same as the keyword, little brother)
Use [EF. DF]
Go
/****** object: Table [dbo].[ User Script Date: 02/05/2012 14:08:41 ******/
set ANSI_NULLS on Go
SET QUOTED_IDENTIFIER
in Go Set Ansi_padding on
go
CREATE TABLE [dbo].[ User] (
[UserId] [int] IDENTITY (1,1) not NULL,
[UserName] [nvarchar] (m) COLLATE NULL,
[UserPassword] [varchar] (m) COLLATE chinese_prc_ci_as NULL,
CONSTRAINT [Pk_user] PRIMARY KEY CLUSTERED
(
[UserId] ASC
) with (Ignore_dup_key = out) on [PRIMARY]
On [PRIMARY] go
SET ansi_padding off
Table UserInfo
Use [EF. DF]
Go
/****** object: Table [dbo].[ UserInfo] Script Date: 02/05/2012 14:21:21 ******/
SET ansi_nulls on
go
SET quoted_identifier
in Go SET ansi_padding on
go
CREATE TABLE [dbo].[ UserInfo] (
[userinfoid] [int] IDENTITY (1,1) not NULL,
[UserId] [int] NULL,
[usertelephone] [varchar] ( COLLATE chinese_prc_ci_as null,
[usermobile] [varchar] (m) COLLATE chinese_prc_ci_as null,
[UserEmail] [ NVARCHAR] (256) COLLATE chinese_prc_ci_as NULL,
CONSTRAINT [pk_userinfo] PRIMARY KEY CLUSTERED
(
[ Userinfoid] ASC
) with (Ignore_dup_key = out) on [PRIMARY]
) on [PRIMARY]
go
SET ansi_padding off
go
Create a relationship
Use [EF. DF]
go
ALTER TABLE [dbo].[ UserInfo] with CHECK ADD CONSTRAINT [Fk_userinfo_user] FOREIGN KEY ([UserId])
REFERENCES [dbo].[ User] ([UserId])
Create a View
Use [EF. DF]
Go
/****** object: View [dbo].[ V_user] Script Date: 02/05/2012 15:23:22 ******/
SET ansi_nulls on
go
SET quoted_identifier
in Go CREATE VIEW [dbo]. [V_user]
As
SELECT dbo.[ User]. UserName, dbo. [User]. UserPassword, dbo. [User]. UserId,
dbo. Userinfo.usertelephone, dbo. Userinfo.usermobile, dbo. Userinfo.useremail
from dbo.[ User] INNER JOIN
dbo. UserInfo
on dbo.[ User]. UserId = dbo. Userinfo.userid Go
creating a stored procedure
--No return value
Use [EF. DF]
set ANSI_NULLS on
go
SET quoted_identifier in
go
--======================================= = = = = = =
Author: <joe cai>-
-Create Date: <2012-2-5>-
Description: < Delete user >
--= = ===========================================
CREATE PROCEDURE [dbo]. [P_deleteuserbyid]
@p_UserId int
as
BEGIN
--When set NOCOUNT is on, does not return a count (representing the number of rows affected by a Transact-SQL statement)
SET NOCOUNT on;
DELETE from dbo. UserInfo WHERE UserId = @p_UserId
DELETE from dbo.[ User WHERE UserId = @p_UserId end
Go
--There are return values
Use [EF. DF]
SET ANSI_NULLS on
SET QUOTED_IDENTIFIER in
go
--============================================= -
-Author: <joe cai>-
-Create Date: <2012-2-5>-
Description: < get user Information >
--=============================================
CREATE PROCEDURE [dbo]. [P_user]
@p_UserId int
as
BEGIN
SELECT *
from
dbo.[ User] left
JOIN
dbo. UserInfo on
dbo.[ User]. UserId = dbo. Userinfo.userid
WHERE [User]. UserId = @p_UserId End
Little Episode
Use of "SET ANSI_NULLS on" and "SET QUOTED_IDENTIFIER on" in SQL Server:
These are SQL-92 setup statements that allow SQL Server 2000/2005 to comply with SQL-92 rules.
"Quoted_identifier"
When SET QUOTED_IDENTIFIER is on, identifiers can be delimited by double quotes, and literals must be delimited by single quotes.
When SET Quoted_identifier is off, identifiers are not quoted and must conform to all Transact-SQL identifier rules.
"Ansi_nulls"
The SQL-92 standard requires that the value be FALSE when the null value is equal to (=) or Not equal to (<>) compared.
When SET Ansi_nulls is on,
A SELECT statement that uses the WHERE column_name = NULL returns 0 rows, even if the column_name contains a null value.
A SELECT statement that uses the WHERE column_name <> NULL returns 0 rows, even if the column_name contains non-null values.
When SET Ansi_nulls is off,
equals (=) and not equal to (<>) comparison operators do not comply with the SQL-92 standard.
A SELECT statement that uses a WHERE COLUMN_NAME = null returns a row in column_name that contains a null value.
The SELECT statement that uses the WHERE column_name <> NULL returns the rows in the column that contain non-null values.
Returns all rows that are not xyz_value or NULL by using a WHERE column_name <> xyz_value SELECT statement.
↓
To generate an Entity Data Model
↓
File is named DF.EDMX
↓
↓
Omit two small steps in the middle ...
↓
To name an entity class as Dfentities
Project Directory
Perfect stored procedures with return values (process simplicity)
Gee, it takes time, now that you've made up your mind ...