New Posts New Posts RSS Feed: Well I know how to save a list of entities. But just wondering how to save just one
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Well I know how to save a list of entities. But just wondering how to save just one

 Post Reply Post Reply
Author
IdeaBlade View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 30-May-2007
Location: United States
Posts: 353
Post Options Post Options   Quote IdeaBlade Quote  Post ReplyReply Direct Link To This Post Topic: Well I know how to save a list of entities. But just wondering how to save just one
    Posted: 16-Jul-2007 at 1:44pm
Make an EntityList.
 
Add the one entity that you want to save to the EntityList.
 
Save the single entity using this signature: SaveChanges(IEnumerable) Method
 
Here is a code example:
 
private void SaveSample() { 
  PersistenceManager pm = PersistenceManager.DefaultManager; 
 
  // Retrieve a single Order and its Customer 
  Order order = pm.GetEntity<Order>(new PrimaryKey(typeof(Order), 10275)); 
  Customer cust = order.Customer; 
 
  // Change the address in both Order and Customer 
  order.ShipAddress = "100 Broadway"; 
  cust.Address = "100 Broadway"; 
 
  // Now save -- using a save list and SaveOptions. 
  EntityList<Entity> changedEntities = new EntityList<Entity>(); 
  changedEntities.Add(order); 
  changedEntities.Add(cust); 
  SaveOptions options = new SaveOptions(); 
  options.IsTransactional = true; 
  options.ThrowExceptionOnSaveFailure = ThrowExceptionRule.Never; 
 
  SaveResult sr = pm.SaveChanges(changedEntities, options); 
  
 
Back to Top
Customer View Drop Down
Senior Member
Senior Member
Avatar
User Submitted Questions to Support

Joined: 30-May-2007
Location: United States
Posts: 260
Post Options Post Options   Quote Customer Quote  Post ReplyReply Direct Link To This Post Posted: 16-Jul-2007 at 1:40pm
Well I know how to save a list of entities. But just wondering how to save just one. Create one entity then add it to the database.
Back to Top
IdeaBlade View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 30-May-2007
Location: United States
Posts: 353
Post Options Post Options   Quote IdeaBlade Quote  Post ReplyReply Direct Link To This Post Posted: 16-Jul-2007 at 1:34pm
I am not sure what you mean by "most efficient".  I assume that you mean "best practice".  If that's what you mean, I would look in the Developer's Guide and the Tutorials.
 
Remember that adding or modifying an entity is a 2 step process:
(1)    Add the entity to the cache or modify an existing entity in the cache
(2)    Save the entity from the cache to the database
 
I have a feeling that I don't quite understand your question, so feel free to respond.
Back to Top
Customer View Drop Down
Senior Member
Senior Member
Avatar
User Submitted Questions to Support

Joined: 30-May-2007
Location: United States
Posts: 260
Post Options Post Options   Quote Customer Quote  Post ReplyReply Direct Link To This Post Posted: 16-Jul-2007 at 1:32pm
What is the most efficient way to add or edit one entity to the database.
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down