Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anyone have any ideas how to make a generic methd to save collections in entity framework. In my webapp i have tons of pages where user edits variable length collections and i end up writing the following code over and over again. Is there any way i can turn it into a generic method, so that those entities that are not in the database are added, those that exist are updated, rest are deleted.


C#
private void SavePhoneNumbers(User user, IList<PhoneNumber> phoneNumbers)
{
    // Get ids of entities that allready exist in db(entities where id is greater than zero).New entities have zero for id.
    var idsOfNumsToKeep = phoneNumbers.Where(p => p.Id > 0).Select(p => p.Id).ToArray();
    
    //Delete entities from db that are not in 'idsOfNumsToKeep'-array, because user
    //has deleted them
    var numsToDelete = context.PhoneNumber.Where(p => p.UserId == user.Id && !idsOfNumsToKeep.Contains(p.Id));

    foreach(var num in numsToDelete)
        context.PhoneNumber.DeleteObject(num);

    foreach (var num in phoneNumbers)
    {
        num.UserId = User.Id;
        // An exsiting number update it
        if(num.Id > 0)
        {
            context.PhoneNumber.Attach(num)
            context.ObjectStateManager.ChangeObjectState(num, EntityState.Modified);
        }
        else{
            // It's a new number add it
            context.PhoneNumber.AddObject(num);
        }
    }
}
Posted
Updated 9-Feb-12 8:48am
v3

1 solution

Your issue is knowing the correct Collection to attach to, I imagine if you made that a parameter, then you could make it work.
 
Share this answer
 
Comments
huotari 9-Feb-12 16:16pm    
Yes ofcourse how stupid of me. Thank you. One question, how would you implement the delete operation without reading all the reords from database. Now i'm reading only the records that i'm going to delete. The only way i can think of how to do the delete operation is to read all the records and check if a record exits with that id in the 'new collection'.
Christian Graus 9-Feb-12 16:25pm    
I am pretty new to EF, but I believe that if you create objects on the fly that have the right unique id, and then do context.PhoneNumber.DeleteObject(num), it will generate the correct SQL to do the deletes for you and ignore the other ( irrelevant ) values.
huotari 9-Feb-12 16:43pm    
Thank you again.

Maybe i explained it poorly. I will try again. In the 'IList<PhoneNumber>'-list i only have the entities to keep and to add, but the only way i can know what to delete, is to get the entities from db that that are related to the user, but are not in this list and it is this query that i don't know how to do. I know how to get the primary key property, by examinig the attributes EF generates. Do you know if it's possible to generate a query from ProperyInfo?
Christian Graus 9-Feb-12 17:04pm    
So you want to delete everything that's not in your list ? Can't you just delete all and then insert afterwards ? Sorry if I'm missing something. And no, I'm not sure.
huotari 9-Feb-12 17:10pm    
Yes ofcourse i could do that, i was wondering if you knew how to do that. Thank you for your replies i'll mark this as resolved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900