1. Introduction to EF code first
To be supplemented
Ii. EF code first simple instance
1. Development Environment and Database description
Development Environment: Visual Studio 2010 ultimate SP1 + SQL Server 2008 r2
Database: northwind
2. InstanceCodeStructure
Structure Description:
APP: Console ApplicationProgram
Data: Data Access
Domain: entity class
3. Install Entity Framework
In the Visual Studio Editor, choose tools> library Package Manager> Package Manager Console. In the Package Manager Console window, run the following statement to install the latest Entity Framework.
PM> install-package entityframework
The app layer and data layer Add references to the entityframework respectively:
After entityframework is installed on the app layer, the app. config and packages. config files are automatically added.
App. config configures the Entity Framework Version and database connection information, and modifies the data connection information to adapt to the local environment.
1 <? XML version = " 1.0 " Encoding = " UTF-8 " ?>
2 <Configuration>
3 <Configsections>
4 <! -- For more information on Entity Framework configuration, visit http: // Go.microsoft.com/fwlink /? Linkid = 237468 -->
5 <Section name = " Entityframework " Type = " System. Data. entity. Internal. configfile. entityframeworksection, entityframework, version = 4.3.1.0, culture = neutral, publickeytoken = b77a5c561934e089 " />
6 </Configsections>
7 <Entityframework>
8 <Defaconnecticonnectionfactory type = " System. Data. entity. Infrastructure. sqlconnectionfactory, entityframework " >
9 <Parameters>
10 <! -- <Parameter value = " Data Source = (localdb) \ v11.0; Integrated Security = true; multipleactiveresultsets = true " /> -->
11 <Parameter value =" Data Source = (local); User ID = sa; Password =; multipleactiveresultsets = true " />
12 </Parameters>
13 </Defaultonfactory>
14 </Entityframework>
15 </Configuration>
Packages. config package used by the current project:
1<? XML version ="1.0"Encoding ="UTF-8"?>
2<Packages>
3<Package ID ="Entityframework"Version ="4.3.1"/>
4</Packages>
4. instance code
Category. CS in domain
1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5
6 Namespace Northwind. domain. Entities
7 {
8 Public Class Category
9 {
10 /// <Summary>
11 /// CATEGORY ID
12 /// </Summary>
13 Public Int Categoryid {Get ; Set ;}
14
15 /// <Summary>
16 /// Category name
17 /// </Summary>
18 Public String Categoryname { Get ; Set ;}
19 }
20 }
Northwindcontext. CS in Data
1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5
6 Using System. Data. entity;
7
8 Using Northwind. domain. entities;
9
10 Namespace Northwind. Data
11 {
12 Public Class Northwindcontext: dbcontext
13 {
14 Public Dbset <Category> categories { Get ; Set ;}
15 }
16 }
Program. CS in app
1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5
6 Using Northwind. Data;
7 Using Northwind. domain. entities;
8
9 Namespace Northwind. app
10 {
11 Class Program
12 {
13 Static Void Main ( String [] ARGs)
14 {
15 Category C = New Category () {categoryname = " Digital Electronics " };
16
17 Using (Northwindcontext DB = New Northwindcontext ())
18 {
19 DB. categories. Add (C );
20 DB. savechanges ();
21 }
22
23 Console. writeline ( " Finish " );
24 Console. readkey ();
25 }
26 }
27 }
5. Running instructions
Since the above database connection string does not contain the specified database name, after the operation is successful, the following database and table will be created in the local data engine:
Database Name: northwind. Data. northwindcontext
Table Name: Categories
6. Sample Code attachment
Northwind.rar