none
LINQ to SQL Update RRS feed

  • Question

  • Hi,
    The changes are not being saved to the database upon committing SubmitChanges.  The in-memory object contains the value but everytime i perform the Get function, it gets the values from DB.  This is how i am getting the list:

    Entity Class
    public partial class Org_EntityType : ParentClass<Org_EntityType>, INotifyPropertyChanging, INotifyPropertyChanged

    Parent Class has Get and Update methods

    public class ParentClass<T>
    {
    public static T Get<T>(Func<T, bool> pType) where T : class
    {
    T record;
    using (OrgstructureDataContext context = new OrgstructureDataContext())
    {
    context.ObjectTrackingEnabled =
    false;
    Table<T> gTable = context.GetTable<T>();
    record = gTable.Where(pType).First<T>();
    }
    return record;
    }

    public

     

    static T Update(T pType)
    {
    u
    sing (OrgstructureDataContext context = new OrgstructureDataContext())
    {
    ITable gTable = context.GetTable(typeof(T));
    gTable.Attach(pType);
    context.SubmitChanges();
    }
    return pType;
    }
    }

    //Calling Get and Update methods
    Org_EntityType oEnType = Org_EntityType.Get<Org_EntityType>(oe => oe.OrgEntityTypeID == 121);
    oEnType.Description = "Test Entity Type"; //original value is 'Test Abstract Type'
    oEnType = Org_EntityType.Update(oEnType);

    Console
    .WriteLine(oEnType.Description); //gives me the changed value 'Test Entity Type'


    However when i do the get again, it returns the original value 'Test Abstract Type'.  I tried Refresh(RefreshMode.KeepChanges,oEntType) but it doesn't do anything.  I tried attaching with modified as true, but it tells me that "An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy."  that looks like it can open another pandora box.  please assist.

    thanks,


     


    kashif
    • Edited by Kashif Pervaiz Wednesday, August 26, 2009 4:14 PM for some reason it keeps duplicating text
    • Moved by OmegaManModerator Wednesday, August 26, 2009 6:37 PM (From:.NET Base Class Library)
    Wednesday, August 26, 2009 4:11 PM

Answers

  • Thank you for your reply.  That doesn't work, but one good thing for the designer is that i can select all the columns in one table, and set the UpdateCheck to Never, and make the following changes in the Update Function.

    public

     

    static T Update(T pType)
    {
    using (OrgstructureDataContext context = new OrgstructureDataContext())
    {
    ITable gTable = context.GetTable(typeof(T));
    gTable.Attach(pType,
    true);
    //context.GetTable(typeof(T)).Attach(pType);
    context.SubmitChanges();
    }
    return pType;
    }


    kashif
    • Marked as answer by Kashif Pervaiz Wednesday, September 2, 2009 5:45 PM
    Wednesday, September 2, 2009 4:02 PM

All replies

  • I believe the object in question is not being properly detached from the originating data context. Try calling this code for the object before calling attach

    pType.PropertyChanged = null;
    
    pType.PropertyChanging = null;

    Note if pType has sub objects associated, you will need to do the same with those objects. HTH
    William Wegerson (www.OmegaCoder.Com)
    Wednesday, August 26, 2009 4:39 PM
    Moderator
  • Hi,

    Thanks for your reply.  I applied what you suggested before calling the update method

    oEntType.PropertyChanged = null;

    oEntType.PropertyChanging = null;

    But i get the one of the following errors:

    The event 'Org_EntityType.PropertyChanging' can only appear on the left hand side of += or -= (except when used from within the type 'Org_EntityType')

    pType does not expose these events as it is passed in as type T.  I modified the dbml to add UpdateCheck attribute to Never and it works fine.  But i don't want to do that for all my columns, there are over 30 tables with minimum of 15 columns each. 


    kashif
    Wednesday, August 26, 2009 4:54 PM
  • please help.. this is just a start. the number of tables will grow exponentially once more modules are added. thanks in advance.
    kashif
    Wednesday, August 26, 2009 8:08 PM
  • Hi Kpbutt,

     

    I’ve look through your code and found that you do not actually attach the T object to the table.

     

    ITable gTable = context.GetTable(typeof(T));
    gTable.Attach(pType);

     

    You just add to the ITable you defined not the context. It may looks like this:

    context.GetTable(typeof(T)).Attach

     

    There are two blog about the attaching object in LINQ to SQL for your reference:

    http://www.west-wind.com/WebLog/ShowPost.aspx?id=134095

    http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx

     

    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng

    Tuesday, September 1, 2009 8:44 AM
  • Thank you for your reply.  That doesn't work, but one good thing for the designer is that i can select all the columns in one table, and set the UpdateCheck to Never, and make the following changes in the Update Function.

    public

     

    static T Update(T pType)
    {
    using (OrgstructureDataContext context = new OrgstructureDataContext())
    {
    ITable gTable = context.GetTable(typeof(T));
    gTable.Attach(pType,
    true);
    //context.GetTable(typeof(T)).Attach(pType);
    context.SubmitChanges();
    }
    return pType;
    }


    kashif
    • Marked as answer by Kashif Pervaiz Wednesday, September 2, 2009 5:45 PM
    Wednesday, September 2, 2009 4:02 PM