none
Deleting entity? RRS feed

  • Question

  • I am definitely missing something. All the documentation that I see on deleting an entity call DeleteObject. But I cannot find this method. Where is it? How do I delete using EF? Intellisense doesn't find the method either.


    Kevin Burton

    Saturday, January 5, 2013 11:04 PM

Answers

  • I fixed the link in my earlier response.

    How many SKUs do you expect to load ? How many SKUs do you expect to delete ? Note also that you do select (and "materialize" that is "create as a .NET object instance" client side) some SKUs (and all their data) you won't delete (i.e the >maxSku criteria could be added to the select statement) and perhaps won't use at all.

    Materializing entities client side makes more sense if you'll do something with them client side. Here this is a batch DELETE server side only  operation that could likely be better done using DbContext.Database.ExecuteSqlCommand there is no need to move data back and forth client side... 


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".



    Sunday, January 6, 2013 4:27 PM

All replies

  • Hi,

    Historically EF provided two APIs : ObjectContext (which was "Model"/"Database first") and DbContext that wraps ObjectContext to add "Code First" support. Could it be that you are using the DbContext API but see ObjectContext based samples ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Proposed as answer by Mr. Javaman II Saturday, January 5, 2013 11:57 PM
    • Unproposed as answer by KevinBurton Sunday, January 6, 2013 3:27 AM
    Saturday, January 5, 2013 11:16 PM
  • I am not sure how to get at the ObjectContext. I see that the context I have is derived from DbContext. Given that how do I delete an entry or convert. I can form a LIXQ query that would list all the objects that I want to remove but I don't know how to remove them. So I have something like:

    var query = from Entity p in context.Entities where p.Field == 5 select p

    foreach(Entity p in query)

    {

    // Remove p

    }


    Kevin Burton

    Sunday, January 6, 2013 3:33 AM
  • When coding against a DbContext you'll use http://msdn.microsoft.com/en-us/library/gg679171(v=vs.103) that is you remove the entity from its DbSet...

    What you saw is likely http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.deleteobject.aspx (you can get an ObjectContext from a DbContext when really needed but there is likely no point in doing that when the DbContext API have what you need).

    Just try to select EF DbContext based tutorials rather than EF ObjectContext based tutorials...

    BTW you'll find DbContext based material at http://msdn.microsoft.com/en-us/data/ee712907 (and DbContext is the new default API even for Database first starting with .NET 4.5/VS 2012).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".



    Sunday, January 6, 2013 12:34 PM
  • On the first link I get:

    Content not found

            We could not find the page you requested.

    Kevin Burton

    Sunday, January 6, 2013 2:18 PM
  • Hi Kevin;

    As Patrice stated in his post use the Remove method of the DbSet to delete an entity in the database on the next save to the database if the entity is in any other state then Added, if the entity is in Added state it will be detached. To delete an entity the command will be like this.

    MyDbContext.MyDbSet.Remove(entity);

    Where MyDbContext is the instance of the DbContext being used and MyDbSet is the collection in the model that point to the table in the database and entity is the object to be removed.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Sunday, January 6, 2013 2:43 PM
  • Any reason that you can think of that calling this Remove method should take an inordinate amount of time? After running for about 5 minutes I finally killed the process becuase it was taking so long. The direct SQL call like:

    DELETE FROM Products
    WHERE CAST(SKU as int) > 814418

    Takes a matter of seconds. Also since int.Parse(SKU) is not recognized I have to do something like:

                var query = from Products p in context.Products where p.i_ClassType == 2 && p.SKU != null select p;
                foreach(Products p in query)
                {
                    if (int.Parse(p.SKU) > maxSku)
                    {
                         context.Products.Remove(p);
                    }
                }
                int rowsAffected = context.SaveChanges();
    

    So between the query taking a long time and the Remove method call taking an even longer time this is not a good solution. Any other ideas?

    Kevin Burton

    Sunday, January 6, 2013 3:46 PM
  • I fixed the link in my earlier response.

    How many SKUs do you expect to load ? How many SKUs do you expect to delete ? Note also that you do select (and "materialize" that is "create as a .NET object instance" client side) some SKUs (and all their data) you won't delete (i.e the >maxSku criteria could be added to the select statement) and perhaps won't use at all.

    Materializing entities client side makes more sense if you'll do something with them client side. Here this is a batch DELETE server side only  operation that could likely be better done using DbContext.Database.ExecuteSqlCommand there is no need to move data back and forth client side... 


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".



    Sunday, January 6, 2013 4:27 PM
  • I really only want to load the SKUs to delete them. There would be about 100-500 of them. So the operation that I am after is entirely on the server side. Thank you for the tip on ExecuteSqlCommand. Does it just take a string of the SQL statement to execute? I am not sure what to supply as the 'parameters'. My first guess would be something like:

    context.Database.ExecuteSqlCommand("DELETE FROM Products WHERE CAST(SKU as int) > 814418", null);

    If I knew how to pass parameters then I would replace the 'null' with something more meaningful like 814418. Comments?

    Kevin Burton

    Sunday, January 6, 2013 11:17 PM
  • One more comment. I am not bowled over by the EF performance for insertion. It seems that EF is a little slower than ADO.NET but it is always in the ball park. My crude attempt at inserting using EF is as follows:

                for (int i = 0; i < loopCount; i++)
                {
                   Products product = Copy(context.Products.Create(), referenceProduct);
                    product.SKU = (currentSku++).ToString();
                    product.oid = currentOid++;
    
                    timer.Restart();
    
                    context.Products.Add(product);
                    context.SaveChanges();
    
                    timer.Stop();
                    stats.Results.Add(timer.ElapsedMilliseconds);
                }
    

    The timer is an instance of the Stopwatch class. 'stats' is a simple class that takes in the timing numbers and finds the mean, standard deviation, min, and max. The 'write' I expect to happen on the two lines 'Products.Add' and 'SaveChanges'. The 'Copy' method simple populates the Product with values from another Product. Following that you will notice that I only change two properties of the Product before it gets written. This all seems to work. I look at the database and there are 'loopcount' new rows inserted just as I expected. The problem is that this takes about 3-4 times longer than the equivalent ADO.NET code. Am I doing something wrong or that is just the way it is? Thanks again.

    Kevin Burton

    Sunday, January 6, 2013 11:59 PM