The Entity Framework, referred to as EF, belongs to one of the data persistence ( persistence ) shelves, and the others are Nhibernate,ibaties,dapper, Petapoco ... And so on, are based on the ORM idea.
First introduce the next O/R Mapping (ORM)
1. What is ORM? ORM refers to the transformation of the object-oriented object model and the data structure of the relational database, which can be understood to convert the table entities and tables to each other (applicable on any platform, such as Php,java, etc.).
Traditional ADO Operation database:
Operation database based on ORM thought:
Next we'll create the project in one step:
I'm using the vs2012. The default is EF5.0, if you do not have a blog park search under How to install, here is not introduced,
First we create a new project to add new items to the project such as:
Click Add:
We choose to generate from the database:
Here we click on New connection:
After the selection is complete, click OK:
Be sure to set "yes" here, you can click "No" to see what is the difference between the entity link string and "Yes", click Next:
Click Done, OK, according to the database generated entities such as:
See what files are generated in the solution Manager:
So let's open the DataModel.Context.cs and see what's Inside:
Let's simply write a crud:
1. Add
// 1. Declaring the context of an EF New demotestentities (); // 2. Declaring an entity New ; // 3. Tell EF to add an action to the above entity AA DbContext.UserInfo.Add (UserInfo); // 4. Tell the context to save the entity's changes in the database to dbcontext.savechanges ();
2. Enquiry
Static voidMain (string[] args) { //Query OperationsDemotestentities DbContext =Newdemotestentities (); List<UserInfo> list = dbContext.UserInfo.Where (U = u.name = ="Little Bastard ."). ToList (); List. ForEach (U=Console.WriteLine (U.tostring ())); Console.readkey (); } Public Partial classuserinfo{ Public Override stringToString ()//rewrite ToString () { return This. Age +","+ This. Name; }}
3. Modifications
Static voidMain (string[] args) { //Modify OperationDemotestentities DbContext =Newdemotestentities (); UserInfo UserInfo=NewUserInfo (); Userinfo.name="Little Bastard ."; Userinfo.age= -; Userinfo.userid=1;//The ID must be specified when the deletion is modified//tells the context to make modifications to this entityDbcontext.entry<userinfo> (UserInfo). State =System.Data.EntityState.Modified; //modify data in a column of a database//dbcontext.entry<userinfo> (UserInfo). property<string> (U = u.name). IsModified = true; //modify data in a column of a database the second way//dbcontext.entry<userinfo> (UserInfo). state = System.Data.EntityState.Unchanged; //dbcontext.entry<userinfo> (UserInfo). Property ("Name"). IsModified = true;dbcontext.savechanges (); }
4. Delete
Static voidMain (string[] args) { //Delete OperationDemotestentities DbContext =Newdemotestentities (); UserInfo UserInfo=NewUserInfo (); Userinfo.name="Little Bastard ."; Userinfo.age= -; Userinfo.userid=1;//The ID must be specified when the deletion is modified//tells context to delete this entityDbcontext.entry<userinfo> (UserInfo). State =System.Data.EntityState.Deleted; Dbcontext.savechanges (); }
View Code
Entity Framework Basics-Second