zoukankan      html  css  js  c++  java
  • Entity Framework Tutorial Basics(21):CRUD Operation in Connected Scenario

    CRUD Operation in Connected Scenario:

    CRUD operation in connected scenario is a fairly easy task because the context automatically tracks the changes that happened in the entity during its lifetime, provided AutoDetectChangesEnabled is true, by default.

    Make sure that you have created an Entity Data Model as shown in the Create Entity Data Modelsection for SchoolDB sample database.

    The following example shows how you can add, update, and delete an entity in the connected scenario (within the scope of context), which in turn will execute insert, update, and delete commands on the database. Context will automatically detect the changes and update the state of an entity.

    using (var context = new SchoolDBEntities())
    {
        var studentList = context.Students.ToList<Student>();
    
        //Perform create operation
        context.Students.Add(new Student() { StudentName = "New Student" });
    
        //Perform Update operation
        Student studentToUpdate = studentList.Where(s => s.StudentName == "student1").FirstOrDefault<Student>();
        studentToUpdate.StudentName = "Edited student1";
    
        //Perform delete operation
        context.Students.Remove(studentList.ElementAt<Student>(0));
    
        //Execute Inser, Update & Delete queries in the database
        context.SaveChanges();
    } 

    Note: If context.Configuration.AutoDetectChangesEnabled = false then context cannot detect changes made to existing entities so do not execute update query. You need to call context.ChangeTracker.DetectChanges() before SaveChanges() in order to detect the edited entities and mark their status as Modified.

    Context detects adding and deleting entity, when the operation is performed only on DbSet. If you perform add and delete entity on a separate collection or list, then it won't detect these changes.

    The following code will NOT insert or delete student. It will only update the student entity because we are adding and deleting entities from the List and not from DbSet.

    using (var context = new SchoolDBEntities())
    {
        var studentList = context.Students.ToList<Student>();
    
        //Add student in list
        studentList.Add(new Student() { StudentName = "New Student" });
    
        //Perform update operation
        Student studentToUpdate = studentList.Where(s => s.StudentName == "Student1").FirstOrDefault<Student>();
        studentToUpdate.StudentName = "Edited student1";
    
        //Delete student from list
        if (studentList.Count > 0)
            studentList.Remove(studentList.ElementAt<Student>(0));
    
        //SaveChanges will only do update operation not add and delete
        context.SaveChanges();
    }

    Learn how to do the CRUD operation in the disconnected scenario in the next sections.

  • 相关阅读:
    linux nat style
    vmware tools install
    linux network
    sql group by max
    实例 | tp5使用七牛云上传图片和文件/删除文件
    菜鸟如何使用composer
    浅谈数据库用户表结构设计,第三方登录
    app登陆,注册,第三方登陆数据设计及业务流程
    数据库设计——评论回复功能
    app接口设计之token的php实现
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5649065.html
Copyright © 2011-2022 走看看