Answered by:
ADO.NET Entity Framework: ObjectContext.customers does not get the newly add new xustomer if not call SaveChanges

Question
-
We use ADO.NET Entity Framework4 and SQL server compact edition.
we notice after we add a new customer by using
ObjectContext.AddToCustomer(newCustomer);
if we do not call ObjectContext.SaveChanges() and call
ObjectContext.Customers will not get the newly added customer.
Since in our application, sometime we need to block ObjectContext.SaveChanges() during database backup,
we need find a way to allow ObjectContext.Customers to get the collection to include newly added customer even though we do not call Savechanges method.
Is there way to can achieve this? thx!
JaneCFriday, March 25, 2011 4:43 PM
Answers
-
Hi Jane,
You can use the ObjectStateManager to find the added customers:
ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added).Select(ose => ose.Entity).OfType<Customer>();
~Rowan
- Proposed as answer by Rowan Miller Friday, March 25, 2011 9:47 PM
- Marked as answer by Alan_chen Tuesday, April 5, 2011 2:54 AM
Friday, March 25, 2011 9:47 PM -
Hello Jane,
I hope this might provide you some insight towards your solution
public void InsertCustomer(Customer customer) { if (customer.Person == String.Empty) { customer.Person = RetrieveSalesPersonForCompany(customer.Name); } if ((customer.EntityState != EntityState.Detached)) { this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added); } else { this.ObjectContext.Customers.AddObject(customer); } }
- Marked as answer by Alan_chen Tuesday, April 5, 2011 2:55 AM
Tuesday, March 29, 2011 10:06 PM
All replies
-
Hi Jane,
You can use the ObjectStateManager to find the added customers:
ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added).Select(ose => ose.Entity).OfType<Customer>();
~Rowan
- Proposed as answer by Rowan Miller Friday, March 25, 2011 9:47 PM
- Marked as answer by Alan_chen Tuesday, April 5, 2011 2:54 AM
Friday, March 25, 2011 9:47 PM -
Hello Jane,
I hope this might provide you some insight towards your solution
public void InsertCustomer(Customer customer) { if (customer.Person == String.Empty) { customer.Person = RetrieveSalesPersonForCompany(customer.Name); } if ((customer.EntityState != EntityState.Detached)) { this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added); } else { this.ObjectContext.Customers.AddObject(customer); } }
- Marked as answer by Alan_chen Tuesday, April 5, 2011 2:55 AM
Tuesday, March 29, 2011 10:06 PM -
Hi Pavan,
thanks for replying our question.
Does anyone know what is difference between ObjectContext.Customers.AddObject(customer) and ObjectContext.AddToCustomer(customer)?
thx!
JaneCWednesday, March 30, 2011 4:46 AM -
Hello Jane,
Both are same but
AddToCustomer(customer) is a strongly typed wrapper around Customers.AddObject(customer)
I would use AddToCustomer(customer) method , as it is strongly typed and gives you compile time type checking.
Hope this helps
Thanks
Pavan
Thursday, March 31, 2011 12:01 AM