Small instance of the entity Framework: Adding an entity class to the project and inserting
1>. Create a console program
2>. Add an ADO. NET Entity Data model, select the corresponding database and table (STUDENTMODEL.EDMX)
3>. Control Table Code
Static voidMain (string[] args) { //Create a gateway interface, TestData is the database nameTestdataentities td =Newtestdataentities (); //Create an Entity object, student is the object that the table maps to, assign it a valueStudent St1 =NewStudent (); St1. StudentID="S4"; St1. Studentname="test1"; St1. Age= -; //add an Entity object to the Gateway interface, insert operationTD. Student.addobject (ST1); //The gateway is saved and changedTD. SaveChanges (); Console.WriteLine ("Add success! "); }
As above STUDENTMODEL.EDMX is the generated entity model is the mapping table, which contains the object in the definition of the table
Entity Framework Additions and deletions change operation
1>. Basic Ibid.
2>. Control Table Code
classProgram {//Create a gateway interface, TestData is a database name, static methods can only invoke static classes StaticTestdataentities stuentities =Newtestdataentities (); Static voidMain (string[] args) { //Create an Entity object, student is the object that the table maps to, assign it a valueStudent St1 =NewStudent (); St1. StudentID="S4"; St1. Studentname="Xiao Ming"; St1. Age= -; //Insertstu (ST1); //Deletestu ("S4"); //Student stu2 = Findstudentbyid ("S4"); //STU2. Studentname = "Xiao Ming"; //Updatestu (STU2); //Console.WriteLine (STU2. Studentname); } //Add Action Public Static voidInsertstu (Student stu) {//add an Entity object to the Gateway interface, insert operationStuEntities.Student.AddObject (STU); //The gateway is saved and changedstuentities.savechanges (); Console.WriteLine ("Add success! ID:"+Stu. StudentID); } //Delete Operation Public Static voidDeletestu (stringID) {Student stu=Findstudentbyid (ID); StuEntities.Student.DeleteObject (Stu); Stuentities.savechanges (); Console.WriteLine ("Delete Success! ID:"+Stu. StudentID); } //Update Action Public Static voidUpdatestu (Student stu) {//where Var is the iqueryable<student> type, inheriting ienumerable<student>//classes that inherit IEnumerable can implement foreach varStudent = fromSinchstuentities.studentwhereS.studentid = =Stu. StudentIDSelects; //get a single entity inside the student collection varOldstu =student. Singleordefault (); //Modify the corresponding property valueOldstu.studentname =Stu. Studentname; Oldstu.age=Stu. Age; //Save Changesstuentities.savechanges (); Console.WriteLine ("Update Successful! ID:"+Stu. StudentID); } //new ways to update your data Public BOOLupdateentity (MvcHotel.Model.Customer entity) {BOOLresult =false; //Add object to contextHe. Attach (entity); //change the state of the newly added object, set to modifiedHe. Objectstatemanager.changeobjectstate (entity, System.Data.EntityState.Modified); if(He.) SaveChanges () >0) {result=true; } returnresult; } //Query Operations Public StaticStudent Findstudentbyid (stringID) {//Way 1:linq to EF//query student based on ID and get a single entity in the collection varStu = ( fromSinchstuentities.studentwhereS.studentid = =IDSelects). Singleordefault (); //Way 2:entity SQL stringsql ="Select Value c from Testdataentities.student as C"; ObjectQuery<Student> query = stuentities.createquery<student>(SQL); Objectresult<Student> results =query. Execute (mergeoption.notracking); //return Stu; returnquery. Singleordefault (); } }
The gateway for manipulating databases in EF
ObjectContext encapsulates the connection between the. NET Framework and the database. This class is used as a gateway for Create, read, update, and delete operations
The ObjectContext class, which interacts with data as objects that are instances of the entity types defined in the EDM
An instance of the ObjectContext class encapsulates the following:
A> the connection to the database, encapsulated as a EntityConnection object.
B> describes the metadata of the model, encapsulated in the form of a MetadataWorkspace object
C> Objectstatemanager object for managing persisted objects in the cache
Small instance of the entity Framework: Adding an entity class to the project and inserting