Print Page | Close Window

Saving just one object

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=772
Printed Date: 25-Mar-2025 at 1:46pm


Topic: Saving just one object
Posted By: BillG
Subject: Saving just one object
Date Posted: 17-Apr-2008 at 8:09am
I have a situation where I just want to save one object.  Basically I am reading a value from a table, updating up and want to write the updated value back to the table. 
 
 
RdbQuery query = new RdbQuery(typeof(LocalInfo));
query.AddClause(LocalInfo.LocalIdEntityColumn, EntityQueryOp.EQ, 1);
LocalInfo = myPM.GetEntity<LocalInfo>(query);
Int32 nextId = local.NextEventNo;
local.NextEventNo = NextId + 1;
myPM.SaveChanges(????????);
return nextId;
 
I only want to save the "local" object.  LocalInfo is the information about the union local current logged into the app.
It doesn't like the myPM.SaveChanges(local).
 
 
Bill
 



Replies:
Posted By: f3rland
Date Posted: 17-Apr-2008 at 8:20am
Try this

[VB]
Public Sub SaveEntity(Of T As Entity)(ByVal pEntity As T)
   Dim oList As New List(Of T)
   oList.Add(pEntity)
   PersistenceManager.DefaultManager.SaveChanges(oList)
End Sub

[C#]
public void SaveEntity<T>(T pEntity) where T : Entity
{
    List<T> oList = new List<T>();
    oList.Add(pEntity);
    PersistenceManager.DefaultManager.SaveChanges(oList);
}


Handle with care when using it with child object... Make sure to save parent first.
Geek


Posted By: DataMan
Date Posted: 17-Apr-2008 at 1:30pm
It's weird that I was looking to do the same thing and this was posted today!
 
Excuse my ignorance in this post...
 
So I modified my code to show the following

EntityList<ReportStorage> mReportStorage = new EntityList<ReportStorage>();
List<Entity> oList = new List<Entity>();
oList.Add(mReportStorage[0]);
mPersMgr.SaveChanges(oList);

I only added the [0] because it worked and I knew that I had created an entity list called ReportStorage.  Can someone give me an explaination on what the [0] is and is there anything stored in [1] or [Xn]?

Thanks


Posted By: DataMan
Date Posted: 17-Apr-2008 at 3:44pm
Ok,  I knew I was on the right track and after talking with Ideablade support we came up with the following code
 
EntityList<Entity> saveList = new EntityList<Entity>();
EntityList<Employee> mChangedEmployee = new EntityList<Employee>();
mChangedEmployee.ReplaceRange(mPersMgr.GetEntities<Employee>(DataRowState.Modified));
mChangedEmployee.AddRange(mPersMgr.GetEntities<Employee>(DataRowState.Added));
mChangedEmployee.AddRange(mPersMgr.GetEntities<Employee>(DataRowState.Deleted));
 
foreach (Entity mChanged in mChangedEmployee)
{
       saveList.Add(mChanged);
}
mPersMgr.SaveChanges(saveList);

Now looking at f3rland's code above it shows passing the entire entity to the SaveEntity routine...  Can you show me an example of calling the SaveEntity routine?  Or is my above code the easiest way?

Thanks



Posted By: f3rland
Date Posted: 18-Apr-2008 at 6:26am
[VB]
SaveEntity(oEmpl)
or
SaveEntity(Of Model.Employee)(oEmpl)

[C#]
SaveEntity(oEmpl);
or
SaveEntity<Model.Employee>(oEmpl);




Print Page | Close Window