none
Need guide line to update data by EF various way RRS feed

  • Question

  • see the code and approaches first


    1St approach
    ----------------
    Student stud;
    //1. Get student from DB
    using (var ctx = new SchoolDBEntities())
    {
        stud = ctx.Students.Where(s => s.StudentName == "New Student1").FirstOrDefault<Student>();
    }

    //2. change student name in disconnected mode (out of ctx scope)
    if (stud != null)
    {
        stud.StudentName = "Updated Student1";
    }

    //save modified entity using new Context
    using (var dbCtx = new SchoolDBEntities())
    {
        //3. Mark entity as modified
        dbCtx.Entry(stud).State = System.Data.Entity.EntityState.Modified;     
            
        //4. call SaveChanges
        dbCtx.SaveChanges();
    }


    2nd approach
    ----------------
    using (var ctx = new SchoolDBContext())
    {
    var stud = (from s in ctx.Students
                where s.StudentName == "Student1"
                select s).FirstOrDefault();

    stud.StudentName = "Updated Student1";

    int num = ctx.SaveChanges();
    }    

    3rd approach
    ----------------
    Student stud = null;
    using (SchoolDBContext ctx = new SchoolDBContext())
    {
        ctx.ContextOptions.LazyLoadingEnabled = false;
                    
        stud = (from s in ctx.Students
                where s.StudentName == " student1"
                select s).FirstOrDefault();

    }

    When we use db.Books.Attach(book) for updating data ?

    using (var db = new MyContextDB())
    {
        var result = db.Books.SingleOrDefault(b => b.BookNumber == bookNumber);
        if (result != null)
        {
            try
            {
                db.Books.Attach(book);
                db.Entry(book).State = EntityState.Modified;
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
    //Out of using scope so ctx has disposed here
    stud.StudentName = "Updated student1";

    using (SchoolDBContext newCtx = new SchoolDBContext())
    {
        newCtx.Students.Attach(stud);
        newCtx.ObjectStateManager.ChangeObjectState(stud, System.Data.EntityState.Modified);
        newCtx.SaveChanges();
    }

    please tell me why different approaches is there to update data?

    Tuesday, November 7, 2017 1:23 PM

Answers

  • please tell me why different approaches is there to update data?

    There only two states EF can be in the connected or disconnected state.

    http://www.c-sharpcorner.com/UploadFile/d87001/connected-and-disconnected-scenario-in-entity-framework/

    <copied>

    There are 2 ways (connected and disconnected) to connect to and work with the Entity Framework. Both ways have their own importance. Let's suppose we want to display some data in a Presentation Layer and we are using some n-tier application, so it would be better to open the context, fetch the data and finally close the context. Since here we have fetched the data and closed the context the entities that we have fetched are no longer tracked and this is the disconnected scenario. For a connected scenario let us suppose we have a Windows service and we are doing some business operations with that entity so we will open the context, loop through all the entities, do our business operations and then save the changes with the same context that we opened in the beginning. In the case of a connected scenario the changes are tracked by the context but in the case of a disconnected scenario we need to inform the context about the state of the entity.

    <end>

    • Marked as answer by Sudip_inn Tuesday, November 7, 2017 9:08 PM
    Tuesday, November 7, 2017 6:46 PM