none
update FK value in object RRS feed

  • Question

  • Hey, 

    When inserting values in the db, there isn't a key generated because the database does so.

    This all works fine but when looking at the objects afterwards i see a FK that has the value 0(zero). What step must i take to change it to the right key?

    Now i'm working with this code that updates the PK afterwards:

    private bool VoerUitOpDatabase(T entiteit, string mode, bool recursief)
            {
                using (DataContext context = MaakContext())
                {
                    try
                    {
                        T detachedentity = EntityDetacher<T>.Detach(entiteit);
                        Table<T> tabel = HaalTabel(context);
    
                        if (mode == "verwijder")
                        {
                            tabel.Attach(detachedentity);
                            context.Refresh(RefreshMode.KeepCurrentValues, detachedentity);
                        }
    
                        IterateEntity(detachedentity, context, tabel, mode, recursief);
    //will iterate to every object, when it finds an association(and //recursief is set) it will create a recursive function that does all this again
    
                        try
                        {
                            ChangeSet set = context.GetChangeSet();
                            context.SubmitChanges();
                            //MaakContext().SubmitChanges();
                        }
                        catch (ChangeConflictException ex)
                        {
                            Console.WriteLine(ex.Message);
                            foreach (ObjectChangeConflict occ in context.ChangeConflicts)
                            {
                                Console.WriteLine("Handling concurrency conflict" + occ.ToString());
                                occ.Resolve(RefreshMode.OverwriteCurrentValues);
                            }
                        }
    
                        //update the PK Id of a object
                        if (mode == "slaop")
                        {
                            PropertyInfo IDPropertyInfo =
                                entiteit.GetType().GetProperty(
                                    context.Mapping.GetTable(typeof(T)).RowType.IdentityMembers[0].Name);
                            IDPropertyInfo.SetValue(entiteit, IDPropertyInfo.GetValue(detachedentity, null), null);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return false;
                    }
                    return true;
                }
            }
    How to correctly update the FK of the objects?

     

    TIA,

    Friday, April 9, 2010 7:39 AM

All replies

  • after checking the debug again i see L2S is setting the primary keys, but in the object "detachedentity".

    Is there a way, without changing the return value(bool), that detachedentity is returned to the object that i was saving?

     

    I hope my question is understandable.

     

    TIA,

    Friday, April 9, 2010 10:08 AM