locked
Content Entity Framework 4.0 C# 2012 RRS feed

  • Question

  • I'm using the Entity Framework to update a record in a database. The table contains a number of columns and I will like to find a short way to perform this update when more than one column needs to be update.

    I came across of using the attach and ApplyCurrentValues

    PressContext

    _db = newPressContext();

    _db.target.Attach(_db.target.Single(c => c.TimeStampRelease == myFile.TimeStampRelease));

    _db.target.ApplyCurrentValues(myFile); It seems not to recognize the ApplyCurrentValues method. Is this is something I need to code in the Content Class? Or if there is any other method beside explicit writing every field to match the column to update in the Database. I do populate a model class with corresponding columns fields of the database. Writing a update statement the old way sounds deprecated.

     _db.SaveChanges();


    michael john ocasio

    Saturday, October 19, 2013 3:06 AM

Answers

  • Hello,

    Welcome to this forum.

    >>_db.target.ApplyCurrentValues(myFile); It seems not to recognize the ApplyCurrentValues method

    Yes, the ApplyCurrentValues() method is not a member of DbContext, but a member of ObjectContext.

    To use the ApplyCurrentValues() method, we need to do a translation like below:

     using (DBContext18_1 db = new DBContext18_1())
                {
                    //db.Database.Create();
                    db.Assets.Load();
    
                    Asset asset = new Asset() { AssetID = 6, Name = "A003", Type = 3 };
                    asset.Type = 4;
    
                    ((IObjectContextAdapter)db).ObjectContext.ApplyCurrentValues("Assets", asset);
    
                    db.SaveChanges();
                }

    Thanks&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.

    • Marked as answer by mjocasio23 Monday, October 21, 2013 4:35 PM
    Monday, October 21, 2013 3:45 AM

All replies

  • Hi Michael,

    Welcome to MSDN forum.

    I think this thread is specific to Data forum. This form is to discuss problems about C# development. So I am helping you to move this thread directly into that forum. Thank you for your understanding.

    Best 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.

    Monday, October 21, 2013 2:36 AM
  • Hello,

    Welcome to this forum.

    >>_db.target.ApplyCurrentValues(myFile); It seems not to recognize the ApplyCurrentValues method

    Yes, the ApplyCurrentValues() method is not a member of DbContext, but a member of ObjectContext.

    To use the ApplyCurrentValues() method, we need to do a translation like below:

     using (DBContext18_1 db = new DBContext18_1())
                {
                    //db.Database.Create();
                    db.Assets.Load();
    
                    Asset asset = new Asset() { AssetID = 6, Name = "A003", Type = 3 };
                    asset.Type = 4;
    
                    ((IObjectContextAdapter)db).ObjectContext.ApplyCurrentValues("Assets", asset);
    
                    db.SaveChanges();
                }

    Thanks&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.

    • Marked as answer by mjocasio23 Monday, October 21, 2013 4:35 PM
    Monday, October 21, 2013 3:45 AM