New Posts New Posts RSS Feed: Saving just one object
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Saving just one object

 Post Reply Post Reply
Author
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Topic: Saving just one object
    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
 
Back to Top
f3rland View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Sep-2007
Location: Canada
Posts: 25
Post Options Post Options   Quote f3rland Quote  Post ReplyReply Direct Link To This Post 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


Edited by f3rland - 17-Apr-2008 at 8:27am
Back to Top
DataMan View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Jul-2007
Location: Canada
Posts: 52
Post Options Post Options   Quote DataMan Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
DataMan View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Jul-2007
Location: Canada
Posts: 52
Post Options Post Options   Quote DataMan Quote  Post ReplyReply Direct Link To This Post 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



Edited by DataMan - 17-Apr-2008 at 3:48pm
Back to Top
f3rland View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Sep-2007
Location: Canada
Posts: 25
Post Options Post Options   Quote f3rland Quote  Post ReplyReply Direct Link To This Post Posted: 18-Apr-2008 at 6:26am
[VB]
SaveEntity(oEmpl)
or
SaveEntity(Of Model.Employee)(oEmpl)

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

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down