This is a migrated thread and some comments may be shown as answers.

[Solved] Update Using OpenAccessContext

5 Answers 203 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Muhammad
Top achievements
Rank 1
Muhammad asked on 20 Apr 2011, 01:43 PM
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()

5 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 21 Apr 2011, 12:39 PM
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
0
Muhammad
Top achievements
Rank 1
answered on 21 Apr 2011, 12:44 PM
Can you please tell me that how can I retrieve that record from database using OpenAccessContext ... and update that record.
0
Zoran
Telerik team
answered on 22 Apr 2011, 08:05 AM
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
0
Muhammad
Top achievements
Rank 1
answered on 05 May 2011, 05:16 PM
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.
0
Zoran
Telerik team
answered on 10 May 2011, 05:18 PM
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.
Tags
LINQ (LINQ specific questions)
Asked by
Muhammad
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Muhammad
Top achievements
Rank 1
Zoran
Telerik team
Share this question
or