I am very confused, i am trying in vain to queue up multiple inserts i have thousands of adds to do so i only want to really do the database once.
I am using .net 4 and entity framework 4 and also added reference to system.data.objects but i still have no overload available for SaveChanges
here is my code:
using (TransactionScope scope = new TransactionScope())
{
using (myDbContext context = new myDbContext)
{
foreach (var p in model)
{
var tempProduct = new Products();
// set a loopable list of available products
IEnumerable<MerchantProductFeedMerchantProd> prod = p.prod;
foreach (var i in prod)
{
var prodText = i.text.FirstOrDefault();
var prodUri = i.uri.FirstOrDefault();
var prodPrice = i.price.FirstOrDefault();
FillTempProduct(feedId, i, tempProduct, supplierId, feedInfo, prodPrice, prodText,
prodUri);
context.Products.Add(tempProduct);
context.SaveChanges(false); // no overload
}
scope.Complete();
context.AcceptAllChanges(); //acceptallchanges not referenced ??
}
}
this is really battering my head now, so any help would be much appreciated.
thanks
The AcceptAllChanges method is useful in the scenario where a transaction has failed and a user wants to retry. If you call SaveChanges() or SaveChanges(true),the EF simply assumes that if its work completes okay, everything is okay, so it will discard the changes it has been tracking, and wait for new changes.
The SaveChanges method of the DbContext prepares the Insert , Update & Delete Queries. It does so by tracking the changes to each of the entities' Context is tracking. Whenever we query the database for entities, the Context retrieves them and mark the entity as Unchanged .
In Entity Framework, the SaveChanges() method internally creates a transaction and wraps all INSERT, UPDATE and DELETE operations under it. Multiple SaveChanges() calls, create separate transactions, perform CRUD operations and then commit each transaction.
SaveChanges()Persists all updates to the data source and resets change tracking in the object context. public: int SaveChanges();
Because you are using DbContext API and these methods are from ObjectContext API. DbContext API is simplified = it is only for simple requirements. If you have more complex requirements you must use ObjectContext API by converting your DbContext
to ObjectContext
instance:
var objectContext = ((IObjectContextAdapter)myDbContext).ObjectContext;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With