none
Where are the AddObject and DeleteObject methods? RRS feed

  • Question

  • I'm new to Entity Framework.  I've set up the Entity Framework models in my project.  When I need to set up methods to add, modify, or delete records I search the Internet for examples of how to do this.  Some of the examples that I find are using methods that don't exist in my entity models: AddObject and DeleteObject.  Were these methods replaced in Entity Framework?

    Add a record - The examples I find say to create a new object, call AddObject, then call SaveChanges.  I don't see the AddObject method.  I see the Add method and SaveChanges.  (I do also see some examples with the Add method)

    Delete a record - The examples say to query the record, call DeleteObject, then call SaveChanges.  I don't see the DeleteObject method.  I see the Remove method and SaveChanges.


    MCSD .NET developer in Dallas, Texas

    Monday, March 23, 2015 2:12 PM

Answers

  • AddObject and DeleteObject are methods of the ObjectContext but in newer versions of Entity Framework you are typically working against a DbContext. This is a different type with different methods.

    You could just add or remove an entity object directly from a DbSet<T> like this:

    var user = new User();
    context.Users.Add(user);
    
    context.Users.Remove(user);
    
    //or:
    
    context.Set<User>().Add(user);  
    
    context.SaveChanges();
    

    Equivalent of ObjectContext.AddObject(entityName, entity) on DbContext: http://stackoverflow.com/questions/14426745/equivalent-of-objectcontext-addobjectentityname-entity-on-dbcontext

    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer then start a new thread if you have a new question.

     

    • Marked as answer by DallasSteve Monday, March 23, 2015 9:24 PM
    Monday, March 23, 2015 4:12 PM

All replies