Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I dont have overload for EF savechanges, and no acceptallchanges available

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

like image 931
davethecoder Avatar asked Dec 07 '11 00:12

davethecoder


People also ask

What's the difference between SaveChanges () and AcceptAllChanges ()?

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.

How do I use SaveChanges in Entity Framework?

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 .

Is SaveChanges a transaction?

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.

What is SaveChanges?

SaveChanges()Persists all updates to the data source and resets change tracking in the object context. public: int SaveChanges();


1 Answers

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;
like image 107
Ladislav Mrnka Avatar answered Sep 28 '22 16:09

Ladislav Mrnka