Print Page | Close Window

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

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=281
Printed Date: 14-Apr-2026 at 4:24pm


Topic: Well I know how to save a list of entities. But just wondering how to save just one
Posted By: Customer
Subject: Well I know how to save a list of entities. But just wondering how to save just one
Date Posted: 16-Jul-2007 at 1:32pm
What is the most efficient way to add or edit one entity to the database.
 



Replies:
Posted By: IdeaBlade
Date 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.


Posted By: Customer
Date 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.


Posted By: IdeaBlade
Date 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); 
  
 



Print Page | Close Window