none
exam 70-516 strange questions... RRS feed

  • Question

  • I had a question at exam:

    You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
    The application connects to a Microsoft SQL Server database.
    You use the ADO.NET Entity Framework to manage persistence-ignorant entities. You create an ObjectContext instance named context.
    Then, you directly modify properties on several entities. You need to save the modified entity values to the database.
    Which code segment should you use?

    A. context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

    B. context.SaveChanges(SaveOptions.DetectChangesBeforeSave);

    C. context.SaveChanges(SaveOptions.None);

    D. context.SaveChanges();

    And I'm confused - what's the correct answer, because both B and D in fact save the changes?

    And btw in the book MCTS Exam 70-516 Training Kit there is a phrase "Be sure to call DetectChanges before prior to calling SaveChanges" - it sounds strange, because SaveChanges internally calls DetectChanges... Strange.

    Thanks

    Monday, December 2, 2013 11:30 AM

Answers

  • Hello,

    >>Then, you directly modify properties on several entities.

    Does it use the ‘AttachTo’ or other similar ways to do this operation like below?

    ObjectContext context = ((IObjectContextAdapter)db).ObjectContext;
    
    
                    Order order = new Order() { OrderID = 1, OrderCode = "C001", OrderName = "C001" };
    
    
                    context.AttachTo("Orders", order);
    
    
                    context.ObjectStateManager.ChangeObjectState(order, EntityState.Modified);
    

    If it is, after a test, I found that all of them could work fine actually.

    >>it sounds strange, because SaveChanges internally calls DetectChanges... Strange.

    It is said that “When working with POCO, you must call the DetectChanges method on the ObjectContext to attach the POCO entity to the ObjectContext”, it is by designed.

    If I have misunderstood, please let me know and I am appreciate that you could share the operation regarding directly modify properties on several entities because I may be wrong.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 3, 2013 5:09 AM
    Moderator

All replies

  • Hello,

    >>Then, you directly modify properties on several entities.

    Does it use the ‘AttachTo’ or other similar ways to do this operation like below?

    ObjectContext context = ((IObjectContextAdapter)db).ObjectContext;
    
    
                    Order order = new Order() { OrderID = 1, OrderCode = "C001", OrderName = "C001" };
    
    
                    context.AttachTo("Orders", order);
    
    
                    context.ObjectStateManager.ChangeObjectState(order, EntityState.Modified);
    

    If it is, after a test, I found that all of them could work fine actually.

    >>it sounds strange, because SaveChanges internally calls DetectChanges... Strange.

    It is said that “When working with POCO, you must call the DetectChanges method on the ObjectContext to attach the POCO entity to the ObjectContext”, it is by designed.

    If I have misunderstood, please let me know and I am appreciate that you could share the operation regarding directly modify properties on several entities because I may be wrong.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 3, 2013 5:09 AM
    Moderator
  • Hello Fred!

    I don't know exactly their scenario... :)

    it's not needed:

    context.ObjectStateManager.ChangeObjectState(order, EntityState.Modified);

    you can attach, change some properties

    order.OrderName = "NewName";

    and SaveChanges (B,D) do save the change

    Tuesday, December 3, 2013 6:53 AM
  • Do you mean that we do a  query operation first and then change the property like below:

     Order order = context.Orders.FirstOrDefault();
    
                    order.OrderName = "NewName";
    
                    //context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
    
                    //context.SaveChanges(SaveOptions.DetectChangesBeforeSave);
    
                    //context.SaveChanges(SaveOptions.None);
    
                    context.SaveChanges();

    However, again all of them will work.

    If we use only attach without context.ObjectStateManager.ChangeObjectState(order, EntityState.Modified),after a

    test, I found all of them will not work.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 3, 2013 7:49 AM
    Moderator
  • Yes, smth like this.

    Are you sure you're using POCO (persistence-ignorant) classes? .None and .AcceptAllChangesAfterSave don't detect changes with POCO.

    without context.ObjectStateManager.ChangeObjectState - yes, SaveChanges works only if properties are changed after AttachTo...

    Tuesday, December 3, 2013 10:42 AM
  • >>Are you sure you're using POCO (persistence-ignorant) classes?

    Yes, it is like below:

    public partial class Order
        {
            public Order()
            {
                this.OrderDetails = new HashSet<OrderDetail>();
            }
        
            public int OrderID { get; set; }
            public string OrderCode { get; set; }
            public string OrderName { get; set; }
        
            public virtual ICollection<OrderDetail> OrderDetails { get; set; }
        }

    It was created by entity framework automatically.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 4, 2013 2:56 AM
    Moderator