Update Using OpenAccessContext

6 posts, 0 answers
  1. Muhammad
    Muhammad avatar
    87 posts
    Member since:
    Oct 2012

    Posted 20 Apr 2011 Link to this post

    I would like to update my record in the database using OpenAccessContext. I would like to update the Group Table in my database but I don't know how to update it Using DBContext. I can't find the dbContext.SumbitChanges() method. I can just see SaveChanges() method. Any Help would be really appreciated.


    Dim txtGroupName As TextBox = e.Item.FindControl("txtGroupName")
                Dim groupName As String = txtGroupName.Text

                Dim data As GridEditFormItem = e.Item
                Dim nID As Integer = Convert.ToInt64(data.ParentItem("ID").Text)

                ffGroup.ID = nID
                ffGroup.GroupName = groupName
                ffGroup.CreatedBy = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo.UserID
                ffGroup.CreatedOn = DateTime.Now
                dbContext.Add(ffGroup)
                dbContext.SaveChanges()

  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 21 Apr 2011 Link to this post

    Hello Muhammad,

     The SaveChanges method is used for committing your changes to the database. You should use it without a problem. 
    The code you have provided is used to add a fresh new entry to one of your tables ( representing the type of your ffGroup variable). If you would like to update an instance you will have to first retrieve it from the database and then alter its properties and call SaveChanges. I believe that our getting started guide available in our online documentation will prove to be a great start for you.

    All the best,
    Petar
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Muhammad
    Muhammad avatar
    87 posts
    Member since:
    Oct 2012

    Posted 21 Apr 2011 Link to this post

    Can you please tell me that how can I retrieve that record from database using OpenAccessContext ... and update that record.
  5. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 22 Apr 2011 Link to this post

    Hi Muhammad,

     You can obtain the existing record using Linq query or by using the GetObjectByKey method. I will show you the later approach in the following code snippet:

    Dim txtGroupName As TextBox = e.Item.FindControl("txtGroupName")
                Dim groupName As String = txtGroupName.Text
      
                Dim data As GridEditFormItem = e.Item
                Dim nID As Integer = Convert.ToInt64(data.ParentItem("ID").Text)
      
                ffGroup = dbContext.GetObjectByKey(New ObjectKey(ffGroup.GetType().Name, nID))
                ffGroup.GroupName = groupName
                ffGroup.CreatedBy = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo.UserID
                ffGroup.CreatedOn = DateTime.Now
                  
                dbContext.SaveChanges()

    So what you should do is call ffGroup = dbContext.GetObjectByKey(NewObjectKey(ffGroup.GetType().Name, nID))

    in order to obtain the ffGroup object with the desired ID. Then you just set the properties as you would like to and call SaveChanges()(without calling dbContext.Add() as you previously did).


    Regards,
    Zoran
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Muhammad
    Muhammad avatar
    87 posts
    Member since:
    Oct 2012

    Posted 05 May 2011 Link to this post

    Thank you so much ... Is it possible that I can retrieve the ID of inserted record ... ? ID is the primary key value in my table.
  7. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 10 May 2011 Link to this post

    Hi Muhammad,

     Yes, accessing the ID property of your object after the SaveChanges() call will give you its exact ID.

    Kind regards,
    Zoran
    the Telerik team
    Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
Back to Top
DevCraft banner