Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Small instance of Entity Framework: add an entity class to the project and insert it

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Small instance of Entity Framework: add an entity class to the project and insert it

1 >. Create a console program

2 >. Add an ADO.NET entity data model and select the corresponding database and table (StudentModel.edmx)

3 >. Control console code

Static void Main (string [] args) {/ / create a gateway interface, TestData is the database name TestDataEntities td = new TestDataEntities (); / / create an entity object, Student is the object mapped from the table, assign it Student st1 = new Student (); st1.StudentID = "S4" St1.StudentName = "test1"; st1.Age = 20; / add entity objects to the gateway interface, insert operation td.Student.AddObject (st1); / / Gateway saves and changes td.SaveChanges (); Console.WriteLine ("added successfully!") ;}

The entity model generated by StudentModel.edmx above is the mapping table, which contains the definition of the object in the table.

Operation of adding, deleting, modifying and searching Entity Framework

1 >. Basically the same as above

2 >. Control console code

Class Program {/ / create a gateway interface, TestData is the database name, static methods can only call the static class static TestDataEntities stuEntities = new TestDataEntities (); static void Main (string [] args) {/ / create an entity object, Student is the object mapped from the table, assign it to Student st1 = new Student (); st1.StudentID = "S4" St1.StudentName = "Xiaoming"; st1.Age = 20; / / InsertStu (st1); / / DeleteStu ("S4"); / / Student stu2 = FindStudentByID ("S4"); / / stu2.StudentName = "Xiaoming"; / / UpdateStu (stu2); / / Console.WriteLine (stu2.StudentName) } / / add operation public static void InsertStu (Student stu) {/ / add entity objects to the gateway interface, insert operation stuEntities.Student.AddObject (stu); / / Gateway saves and changes stuEntities.SaveChanges (); Console.WriteLine ("added successfully! ID: "+ stu.StudentID);} / / delete operation public static void DeleteStu (string ID) {Student stu = FindStudentByID (ID); stuEntities.Student.DeleteObject (stu); stuEntities.SaveChanges (); Console.WriteLine (" deleted successfully! ID: "+ stu.StudentID);} / / Update operation public static void UpdateStu (Student stu) {/ / where var is an IQueryable type, and classes that inherit IEnumerable / / inherit IEnumerable can implement foreach var student = from s in stuEntities.Student where s.StudentID = = stu.StudentID select s / / get the single entity var oldStu = student.SingleOrDefault () in the student collection; / / modify the corresponding attribute value oldStu.StudentName = stu.StudentName; oldStu.Age = stu.Age; / / save the modified stuEntities.SaveChanges (); Console.WriteLine ("updated successfully! ID: "+ stu.StudentID);} / / New data update method public bool UpdateEntity (MvcHotel.Model.Customer entity) {bool result = false; / / add objects to context he.Attach (entity) / / change the state of the newly added object to modified he.ObjectStateManager.ChangeObjectState (entity, System.Data.EntityState.Modified); if (he.SaveChanges () > 0) {result = true;} return result } / / query operation public static Student FindStudentByID (string ID) {/ / Mode 1: Linq to EF / / query Student according to ID and get a single entity in the collection var stu = (from s in stuEntities.Student where s.StudentID = = ID select s) .SingleOrDefault () / / Mode 2: Entity SQL string sql = "select Value c from TestDataEntities.Student as c"; ObjectQuery query = stuEntities.CreateQuery (sql); ObjectResult results = query.Execute (MergeOption.NoTracking); / / return stu; return query.SingleOrDefault ();}}

Gateways for operating 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 is the main class and is used to interact with data as objects that are instances of entity types defined in EDM

An instance of the ObjectContext class encapsulates the following:

A > connections to the database are encapsulated in the form of EntityConnection objects.

B > metadata that describes the model, encapsulated in the form of MetadataWorkspace objects

C > ObjectStateManager objects used to manage persistent objects in the cache

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report