Start Learning EF, from the Internet to find a lot of, are not what they want, so learn to learn while the process of writing down, for reference.
Operating environment:vs2013+sqlserver2012
Entity Framework4.1 before EF supported the "Database first" and "Model first" programming, EF began to support "code-first" programming from EF4.1, so take a quick look at the EF three programming options today.
Before we begin to introduce these three kinds of EF operations, first establish a database connection in Visual Studio 2013, where we take the "EFDemo" database as an example:
Description: Here I use lenovo\sqlexpress This database, of course, with localdb can also.
Database First
The "Database First" mode we call "DB priority", provided that your application already has a corresponding database, you can use the EF design tool to generate data data classes based on the database, and you can use the Visual Studio Model Designer to modify the correspondence between these models.
First create a console application, then right-click Add New Item, select "ADO. Data Model", name input Efdemodb:
Then choose Generate from Database:
Next Select the database connection and select the EFDemo database that we just built .
not in the dropdown box, we click New Connection
Then select the table
After the model is created
You will find that Visual Studio automatically generates "class," "Student" two entity classes and a "EFDEMODB" database Context action class for you:
Here's a quick look at how to use EF for data queries, and we can see how elegant EF is for data manipulation in the following code:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceEFDemo8 {9 class ProgramTen { One Static voidMain (string[] args) A { -Efdemoentities db =Newefdemoentities (); - the //Sort by ID, and find - varModel = db. Classes.orderby (M = m.id). Select (M =New - { -ID =M.id, +Name =M.name - }); + A at if(model. Count () >0) - { -Console.WriteLine ("ID Number: {0}", model. First (). ID); -Console.WriteLine ("class name: {0}", model. First (). Name); - } - } in } -}
Operation Result:
Note: If your database table structure changes, simply right-click in the blank of the model Design view, select "Update model from database" and follow the wizard. such as
Most of this article is taken from Tri Jiangtao (kenshincui), written by the blog
Entity Framework 5.0series ofEFOverview
EF three Programming details tutorial (C#+EF)